1833875 Members
1639 Online
110063 Solutions
New Discussion

Re: sftp script

 
Sftp script
Occasional Contributor

sftp script

Hi,
I have set up an sftp script and put it as a cronjob to run at 1:00 am. Now the problem is that sometimes the sftp gets stalled what is the best way to set it such that if the process is running for more than 2 hours stop it and start transfer again.
#########################################
#!/bin/ksh cd /source-directory
trafile=$(ls -altr filename | tail -n1 | awk '{print $9}')
echo "OK, starting...."
sftp machine-name << EOF
cd /destination-directory
put $trafile
quit
EOF
####################################


Regards
George
4 REPLIES 4
Muthukumar_5
Honored Contributor

Re: sftp script

Use background processing sftp execution to get control to that sftp action.

Try as,

#!/bin/ksh

sftp()
{
sftp machine-name << EOF
cd /destination-directory
put $trafile
quit
EOF
}

cd /source-directory
trafile=$(ls -altr filename | tail -n1 | awk '{print $9}')
echo "OK, starting...."
sftp 1>/dev/null 2>&1 &
pid=$!
time=0

# 7200 is two hours in seconds

if [[ ! -z ps -ef | grep -v grep | grep $pid ]]
then
while [[ $time -eq 7200 ]]
do
sleep 10
if [[ -z ps -ef | grep -v grep | grep $pid ]]
then
let time=7200;
fi
let time=time+10
done
kill -9 $pid
fi

# END
exit 0

It will do that.
Easy to suggest when don't know about the problem!
Sftp script
Occasional Contributor

Re: sftp script

Thanks Muthukumar,
But is there any better way of kickstarting the stalled sftp process?
Raj D.
Honored Contributor

Re: sftp script

Hi George ,

You can use the expect utility , and write the script ,

Here is a good link may may need to refer:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=964681

Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: sftp script

George,

Here is the link for expect ,hope it will help,

http://expect.nist.gov/

Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "