Operating System - HP-UX
1752754 Members
4565 Online
108789 Solutions
New Discussion юеВ

Re: How to setup an email alert notification for failed SFTP jobs?

 
SOLVED
Go to solution
Jan Shu
Regular Advisor

How to setup an email alert notification for failed SFTP jobs?

Hi,

My DBA setup a sftp cron job to upload data to a remote sftp server. How to setup an email alert notification for failed SFTP jobs? so my DBA can resend the data transfer. Can you please help? Thank you.

Regards,
Jan
23 REPLIES 23
James R. Ferguson
Acclaimed Contributor
Solution

Re: How to setup an email alert notification for failed SFTP jobs?

Hi Jan:

If you SFTP script returns zero for successs and non-zero for failure, then you simply do something like:

# /home/jan/script || mailx -s "Failure in script" root < /dev/null

Regards!

...JRF...
Jan Shu
Regular Advisor

Re: How to setup an email alert notification for failed SFTP jobs?

Dear James,
Thanks for the reply. I asked my DBA if her sftp script returns zero and non-zero for job status. DBA isn't sure, but she shows me the sftp log, it has only the verbes of the sftp connection. I think by default a script always return zero or non-zero for its result, right? Is there a way to test it?

Thanks.

Regards,
Jan
OldSchool
Honored Contributor

Re: How to setup an email alert notification for failed SFTP jobs?

sftp, like ftp, appears to always return a zero, provided there isn't an error in sftp itself.

testing via the command line, and "echo $?" will show 0 regardless, you can prove this by trying to copy an non-existent file on the command line. the "script" will have to be written such that it scans to output log or takes some other action to confirm the file transfered completely / successfully. based on that action, the script should then return a zero or non-zero exit code.....
Jan Shu
Regular Advisor

Re: How to setup an email alert notification for failed SFTP jobs?

Dear OldSchool,

Thanks for the information. I will let my DBA test this. Please wait for my update soon. Thanks again.

Regards,
Jan
James R. Ferguson
Acclaimed Contributor

Re: How to setup an email alert notification for failed SFTP jobs?

Hi (again):

As OldSchool notes, the return value of (S)FTP isn't going to help. You need to capture the session dialog in a file and parse it for success or failure.

If, however, your (S)FTP script were written in Perl, the Perl module implementations allow simple tests like:

...
$sftp->put( $localfile, $remotefile );
if ( $sftp->error ) {
warn "Can't put '$localfile' -> '$remotefile': " . $sftp->error . "\n";
...

Regards!

...JRF...
Jan Shu
Regular Advisor

Re: How to setup an email alert notification for failed SFTP jobs?

Dear James,

I will check with my DBA if their sftp script is in Perl or not. I will search this forum website and hopefully I can find some sample sftp scripts in this forum website.

Thanks again.

Jan
Steven E. Protter
Exalted Contributor

Re: How to setup an email alert notification for failed SFTP jobs?

Shalom,

You could go as simple as this:

If the sftp job fails send an email.

If its a shell script.

sftp or scp command ...
rc=$?
#This gets you the return code.

if [ $rc -ne 0 ]
then
echo "File transfer error" | mailx -s "subject" email@yuor.net
fi

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Jan Shu
Regular Advisor

Re: How to setup an email alert notification for failed SFTP jobs?

Dear Stephen,

Thanks a lot, I will try this too. Please wait for my update.

Have a nice day.

Regards,
Jan
James R. Ferguson
Acclaimed Contributor

Re: How to setup an email alert notification for failed SFTP jobs?

Hi (again) Jan:

Re-read OldShool's post. A simple return code from SFTP in a shell script is _not_ going to work:

Consider:

# cat ./mysh
#!/usr/bin/sh
sftp somehost << EOF!
get /no/file /tmp/nofile
quit
EOF!
RC=$?
echo "rc=${RC}"

# ./mysh
Connecting to somehost...
sftp> get /no/file /tmp/nofile
Couldn't stat remote file: No such file or directory
File "/no/file" not found.
sftp> quit
rc=0

Regards!

...JRF...