Just a Unix suggestion, avoid using $rc as the variable to hold the
return code.
Ponder the results if the code was typoed as
rc = $?
This would actually run the startup script rc if it's in your path and
executable. Not to far fetched.
Instead, use $ret and
ret=$?
Kevin
-----Original Message-----
From: ken farmer
[mailto:db2udbdba-ezmlmshield-x62928304.[Email address protected]
Sent: Wednesday, January 18, 2006 10:31 AM
To: LazyDBA Discussion
Subject: Re: UNIX script checking for FTP
McNeer Mike wrote:
> I have a very small script that transfers a log file via scp to
another
> server. I am in need of a very small piece of code that will give me
> error checking for successful transfer and ability for email
> notification if it should fail. This is on Solaris semi latest version
I
> think 2.7? I am very new to the Unix side of things so go easy on me
> please :-) Any assistance would be appreciated.
Mike,
scp should return an error parent if it fails. Here's an example of
what that would probably look like in korn shell, should be essentially
the same in bourne or bash. You could also do it via python, ruby, etc
- where you'd have cleaner syntax but might have to find the right
modules to support ssh.
echo " ---------------------------------------------------------"
echo " send log files to log archive"
echo " ---------------------------------------------------------"
scp /logdir/myfile.log [Email Address Removed] [ $rc -ne 0 ]; then
echo " ERROR: scp failed with rc=$rc"
mail -s "log archival scp step failed with rc=$rc!" [Email address
protected]
rc=$?
if [ $rc -ne 0 ]; then
echo "ERROR: mail failed with rc=$rc!!!"
fi
exit 2
fi
A couple of things to note:
If there's a downstream process that is polling for this file, it might
pick it up before you've completed sending it. In that case it's
usually base to send it with a temporary name, then change the name
after it's complete. Name changing can be done via sftp.
You could also set up this process to poll for an available log file to
send. This enables it to be somewhat auto-recovering. However, it will
also need a name-changing step, and this can't be atomic to issue
mentioned in point #1. So, there's a little bit of complexity there -
in case one fails and the other succeeds. You would also want to ensure
that you never got two processes running simultaneously due to some kind
of hung process- this can be done via simple pid-checking.
Unless you're using a key with a blank passphrase or an ssh-agent you'll
get prompted for a password. Of the two options the blank passphrase is
easiest, but the agent method is more secure.
Regards,
Ken Farmer
---------------------------------------------------------------------
PLEASE CLICK REPLY-ALL TO SEND A REPLY TO EVERYONE
website: http://www.LazyDBA.com
To unsubscribe: http://www.lazydba.com/unsubscribe.html
DB2 & UDB email list listserv db2-l LazyDBA home page