Script FTP maintain error handling


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script FTP maintain error handling
# 1  
Old 04-28-2014
Script FTP maintain error handling

Hi,

I have ftp script like below
Quote:
#!bin/sh
#***** Set local working directory *****
cd /path/to/script/and/files

#***** Set ftp server, credentials, and file(s) to search for *****
HOST='SERVERADDRESS'
USER='FTPUSERNAME'
PASSWD='FTPPASSWORD'
FILE='FTP_*.dat'

#***** Send files to FTP server *****
ftp -vn $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
prompt n
mput $FILE
quit
END_SCRIPT
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

Last edited by edydsuranta; 05-09-2014 at 04:04 PM..
# 2  
Old 04-28-2014
You can check the return status of the ftp command. Something like:
Code:
#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.
# 3  
Old 04-28-2014
you can return the verbose of ftp and grep it.

Code:
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

# 4  
Old 04-28-2014
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
# 5  
Old 05-09-2014
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
# 6  
Old 05-12-2014
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

error situation handling when starting app from a script

Hi, in my bash script I start 3rd party app that runs quickly normally and returns some data that I use later for processing. However if there are problems the app doesn't return anything and just hangs... then my script also hangs of course, awaiting for the app to comeback. Was wondering how to... (3 Replies)
Discussion started by: TomSu
3 Replies

2. Shell Programming and Scripting

Help me add Error Handling to my script

Hi all, I have some sections of a script that I am trying to add error handling to. Basically if it returns any error, just continue. This is for a deployment script that i am writing to assist in the deployment of scripts out to other systems. At the top of my KSH script i added this... (5 Replies)
Discussion started by: nitrobass24
5 Replies

3. Shell Programming and Scripting

Expect Script Error Handling

Good Day Everyone, I was hoping to get a little insight into an expect script that I've written. Basically we have this expect script to perform an sftp upload, key authentication is not an option, and sftp is the only method supported by our vendor, thus the need for this. I want to be... (3 Replies)
Discussion started by: thaller
3 Replies

4. UNIX for Dummies Questions & Answers

File not found handling in ftp script

I have a pattern for filename to be searched. I need to get the files from remote server Who are matching the file pattern. And i need to exit with non zero return code for: 1)No files found matching that pattern 2)More than one files matching the name pattern. If only one files is... (1 Reply)
Discussion started by: pandeesh
1 Replies

5. Shell Programming and Scripting

Help with Error Handling on Script

Hi, I need your guys help again. I run a script which check for some process status in a loop. when i check the process some of the process could throw an error, how can i check that inside my script. Thanks, RR (3 Replies)
Discussion started by: rrb2009
3 Replies

6. Solaris

FTP error handling - critical

Hi all, I have done google on the aforementioned subject. But didn't find any authentic way of checking 100% Successful FTP transaction. In my case I have critical backup of source code on daily basis. I have shell script which will: 1: "mput" backup files to remote server using FTP 2:... (8 Replies)
Discussion started by: viki250
8 Replies

7. Shell Programming and Scripting

Issue with Error handling,not able to continue the script further

Hi, I am trying to write a script to cleanup files in a log directory .. cd log find Datk** -mtime +7 -exec rm -f {} \; 2> /dev/null Have used the above to clean up files in log directory more then 7 days older. The file can be something like ( auto-generate by some processes and... (2 Replies)
Discussion started by: nss280
2 Replies

8. UNIX for Dummies Questions & Answers

maintain database script...

Hi there. i'm new user at here I need help for this. I need to write a script that maintains a database of cryptographic checksums. In other words, I need this script to check the files checksum whether the files has been modified or not. But i got no idea where to start. Hope anyone here can... (8 Replies)
Discussion started by: hihihehe
8 Replies

9. Shell Programming and Scripting

Handling ftp error

I have a script which connects to remote server and ftp the files It works fine, however if there is any failure in ftp connection can it be handled??? ftp log ftp session start time is: Thu Jun 19 00:00:02 BST 2008 Not connected. Not connected. Interactive mode off. Not connected.... (1 Reply)
Discussion started by: vivek_damodaran
1 Replies

10. Shell Programming and Scripting

Maintain full path of a script in a var when sourcing it from a different script

Hi All, I've searched through the forum for a solution to this problem, but I haven't found anything. I have 2 script files that are in different directories. My first script, let's call it "/one/two/a.sh" looks like this: #!/bin/sh IN_DIR=`dirname $0` CUR_DIR=`pwd` cd $IN_DIR... (4 Replies)
Discussion started by: mrbluegreen
4 Replies
Login or Register to Ask a Question