Script FTP maintain error handling

Hi,

I have ftp script like below

How to insert an error handling, If the transfer failed then send mail to me.
Actually, I just need the script to send an email if the FTP failed. How to put the email script within FTP script?

Thank You
Edy

You can check the return status of the ftp command. Something like:

#You script
quit
END_SCRIPT 
if [ $? -ne 0 ]
then
  #Do your error handling 
fi

Or maybe you can even check if the file has been transferred to your remote.

you can return the verbose of ftp and grep it.

error=$(ftp -vn $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
prompt n
mput $FILE
quit
END_SCRIPT)
if [ $(echo $error | grep 'failed') = "failed" ]; then your handling;fi

Typically, ftp will issue you an error if there is a problem with the ftp command execution itself, but I presume you want error handling for the actual file transfers. That gives you a slight problem, but if you can put on a -v flag, then hopefully the output will be studded with three digit response codes.

Those below 400 are normal successful execution, those 400 & above are errors.

If you capture all the output into a file, you can search that file for errors. These will be lines starting 400 or higher, along with anything saying any of the following

  • Not connected
  • cannot access
  • not found
  • No such file

However, you will have to exclude the success output from a good transfer that just happens to have a few hundred bytes, e.g.:- 499 bytes sent is not an error.

The way we do it is to have a list of questionable items to search for and then a list of exclusions from that list. We pass the output through both tests and then we can work out whether we've done it or not. Probably best to move one file at a time too if you can. If you have lots to move, just call them in a loop and you immediately have more information about which one has failed.

Have a go an let us know how you get on.

I hope that this helps,
Robin
Liverpool/Blackburn
UK

Hi there,

Thank you for your response. Actually, I just need the script to send an email if the FTP failed. How to put the email script within FTP script?

Thank You
Edy

Firstly you have to determine if the transfer attempt failed as I was trying to help you with above, then you can send the alert however you choose.

As I tried to explain, the ftp command will exit with a normal/zero return code if the ftp execution is valid, if if the transfer failed, there are no files, etc. You need to determine what you consider is a failure for you.

What have you adjusted in your code and where are we up to?

Regards,
Robin