Link to home
Start Free TrialLog in
Avatar of sfuk
sfukFlag for United States of America

asked on

How to trap FTP error

Dear Experts,

I have the following script. It set as a cron job. It works fine if the file is present on the server, but if it is not, the script hangs.
How can I trap the failure and then exit from the script?
Avatar of Darrell Porter
Darrell Porter
Flag of United States of America image

do an ls on the ftp server then perform a find or grep on "abc" or some known substring within the filename.
If no lines are returned, the file does not exist.
Avatar of sfuk

ASKER

Thank you, WalkaboutTigger, for your quick response.

When i open ftp process, can I do something like

echo `ls -l abc*` > abc.log
Avatar of sfuk

ASKER

I made the following changes/additions in my initial script and solved the problem.


#! /bin/bash

v_rundir="/home/abc"
v_ftp=`cat ftp.log | grep "not found" | wc -l`
cd $v_rundir
 
rm ftp.log

# Test to see if this process is already running...
if [ -e process.lck ] ; then   # if it is, then exit...
 exit
fi
echo $$ > process.lck     # otherwise, lock this process from duplicated execution...

#FTP Function
# Get the new pgp files...
sftp username@ftp4.server.com <<EOF >>/home/abc/ftp.log 2>&1
cd abc/Toabc
mget abc*.txt.pgp
rm abc*.txt.pgp
quit
EOF
if [ $v_ftp -ne "0" ]; then
rm process.lck
exit 1 
fi
exit

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.