Operating System - HP-UX
1825781 Members
2077 Online
109687 Solutions
New Discussion

How to retry FTP once failed to connect to remote site in unix script

 
SOLVED
Go to solution

How to retry FTP once failed to connect to remote site in unix script

Hi!

I have created a script to ftp to remote site to transfer some files. This script is working fine when remote site is connected but my problems here is that whenever i encounter connection failed due to some error, the script will not retry to ftp to remote site to trnasfer files. Is there a way to solve this problmes.

Thank you
7 REPLIES 7
Steven Sim Kok Leong
Honored Contributor

Re: How to retry FTP once failed to connect to remote site in unix script

Hi,

Add the following condition into your script:

If you would like to retry once only, then
==========================================
if echo quit|ftp -nv $FTP_SERVER|grep "Connection refused" >/dev/null 2>/dev/null
then
echo FTP server is not responding
echo Retrying after 10 mins...
sleep 600
else
echo Connected. Initiating FTP transfer...
ftp ...
fi
==========================================

If you would like it to retry again and again until connection, then
==========================================
while ! echo quit|ftp -nv localhost|grep "Connection refused" >/dev/null 2>/dev/null
do
echo Retrying after 10 mins...
sleep 600
done
echo Connected. Initiating FTP transfer...
ftp ...
==========================================

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: How to retry FTP once failed to connect to remote site in unix script

Hi,

2 typos. Please use below. Add the following condition into your script:

A) If you would like to retry once only, then
==========================================
FTP_SERVER=ftp.singnet.com.sg # replace with actual site

if echo quit|ftp -nv $FTP_SERVER|grep "Connection refused" >/dev/null 2>/dev/null
then
echo $FTP_SERVER is not responding
echo Retrying after 10 mins...
sleep 600
echo Connected. Initiating FTP transfer...
ftp $FTP_SERVER ...
else
echo Connected. Initiating FTP transfer...
ftp $FTP_SERVER ...
fi
==========================================

B) If you would like it to retry again and again until connection, then
==========================================
FTP_SERVER=ftp.singnet.com.sg # replace with actual site

while ! echo quit|ftp -nv $FTP_SERVER|grep "Connection refused" >/dev/null 2>/dev/null
do
echo $FTP_SERVER is not responding
echo Retrying after 10 mins...
sleep 600
done
echo Connected. Initiating FTP transfer...
ftp $FTP_SERVER ...
==========================================

Hope this helps. Regards.

Steven Sim Kok Leong

Steven Sim Kok Leong
Honored Contributor

Re: How to retry FTP once failed to connect to remote site in unix script

Hi,

Can't believe that there are still more typos. :P

Please use below. Add the following condition into your script:

A) If you would like to retry once only, then
==========================================
FTP_SERVER=ftp.singnet.com.sg # replace with actual site

if echo quit|ftp -nv $FTP_SERVER|grep "Connection refused" >/dev/null 2>/dev/null
then
echo $FTP_SERVER is not responding
echo Retrying after 10 mins...
sleep 600
echo 2nd attempt to $FTP_SERVER...
ftp $FTP_SERVER ...
else
echo Connected. Initiating FTP transfer...
ftp $FTP_SERVER ...
fi
==========================================

B) If you would like it to retry again and again until connection, then
==========================================
FTP_SERVER=ftp.singnet.com.sg # replace with actual site

while :;
do
if echo quit|ftp -nv $FTP_SERVER|grep "Connection refused" >/dev/null 2>/dev/null
then
echo $FTP_SERVER is not responding
echo Retrying after 10 mins...
sleep 600
else
echo Connected. Initiating FTP transfer...
ftp $FTP_SERVER ...
exit 0
fi
done
==========================================

Hope this helps. Regards.

Steven Sim Kok Leong
Scott Van Kalken
Esteemed Contributor

Re: How to retry FTP once failed to connect to remote site in unix script

You could also do it from another perspective:

write something to check for the existence of the file AFTER the get. If it's the right size, ends the right way, looks godd whatever, then don't bother to retry, otherwise, just delete and get again.

Scott.

Re: How to retry FTP once failed to connect to remote site in unix script

Hi! Steven,

Thanks for your reply. I have try your solution but having some problems. Here is the message i get :

ftp: connect : Connection timed out
Connected.Initating FTP transfer
ftp: connect : Connection timed out
#


Below is the script file that i created:

#
LOG=/tmp/test.log
PutFname=/tmp/test
cd /tmp
date > $LOG
while :;
do
if echo quit|ftp -nv 10.11.12.13|grep "Connection refused" >/dev/null 2>/dev/null
then
echo 10.11.12.13 is not responding
echo Retying after 10 mins.....
sleep 600
else
echo Connected.Initiating FTP transfer
ftp -i -v -n << eof >> $LOG
open 10.11.12.13
user userid passwd
put $PutFname
quit
eof
date >> $LOG
exit 0
fi
done

Thank you

Regards,

Frankie
Steven Sim Kok Leong
Honored Contributor
Solution

Re: How to retry FTP once failed to connect to remote site in unix script

Hi,

Since the FTP error messages are not pertaining to "Connection refused" only ie. also includes "Connection timed out" etc., the scripts can be modified as follows:

A) If you would like to retry once only, then
==========================================
FTP_SERVER=ftp.singnet.com.sg # replace with actual site

if echo quit|ftp -nv $FTP_SERVER|grep "Connected to" >/dev/null 2>/dev/null
then
echo Connected. Initiating FTP transfer...
ftp $FTP_SERVER ...
else
echo $FTP_SERVER is not responding
echo Retrying after 10 mins...
sleep 600
echo 2nd attempt to $FTP_SERVER...
ftp $FTP_SERVER ...
fi
==========================================

B) If you would like it to retry again and again until connection, then
==========================================
FTP_SERVER=ftp.singnet.com.sg # replace with actual site

while :;
do
if echo quit|ftp -nv $FTP_SERVER|grep "Connected to" >/dev/null 2>/dev/null
then
echo Connected. Initiating FTP transfer...
ftp $FTP_SERVER ...
exit 0
else
echo $FTP_SERVER is not responding
echo Retrying after 10 mins...
sleep 600
fi
done
==========================================

Hope this helps. Regards.

Steven Sim Kok Leong

Re: How to retry FTP once failed to connect to remote site in unix script

Hi! Steven, Scott:

My script is working fine now. Thanks for all the help.

Regards,

Frankie