The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Use of TRAP Command Deepakh Shell Programming and Scripting 2 12-12-2006 01:24 AM
trap command mobile01 UNIX for Dummies Questions & Answers 1 11-30-2006 08:54 AM
trap command onlyc UNIX for Dummies Questions & Answers 3 07-11-2006 12:15 AM
Trap command problem superprogrammer Shell Programming and Scripting 3 06-06-2005 06:50 AM
Using TRAP command dbrundrett Shell Programming and Scripting 3 07-15-2004 06:25 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-11-2008
Registered User
 

Join Date: Jan 2008
Posts: 51
how to use trap command in shell script

Right now I have implemented autossh between ServerA & ServerB which are sun solaris based. I have made this shell script. I am facing one problem which I am going to discuss now.
The problem is when I sftp some files (suppose there is 10 files I have to transfer through sftp) from one server
to another. During sftp if suppose network goes down then the process get hang and it does not transfer further files.
But if network goes down during comparing the files then only that file didnot get transfer and it means it will sftp other 9 files.

One strange thing also find that if suppose during transfer of files network goes down, if the network goes up within
5 min of goes down then it will sftp all the files. But if network goes up after five minutes say 10 min. then either
process get hang or sftp all the files execpt one during which network goes down.

I guess, I have to use trap command if the network goes down during transfer of files through sftp command. so that my
shell program should not hang and control should back to new statements of shell script (means statement after sftp statement). Could any body tell me how to use trap command so that my shell script won't get hang.

I am bit new to Shell scripting and I don't no how to incorporate trap command in this shell script


Code:
#! /bin/sh

exec < /girish/server # server file will contain only ip address of the other server
read IP

cd /anshu

for file in `ls -1rt`
do

while true
do

var=`ping $IP | cut -d " " -f3`

if [ $var = "alive" ]
then
echo $var 
echo "lcd /anshu \n cd /gir \n mput $file \n bye" | sftp -B 131072 -v $IP 1>/girish/sftp1.log 2>/girish/sftp2.log
grep -i Uploading /girish/sftp1.log >/girish/output1
grep -i transfer /girish/sftp2.log >/girish/output2

echo "lcd /amit \n cd /gir \n mget $file \n bye" | sftp -B 131072 -v $IP 1>/girish/sftp3.log 2>/girish/sftp4.log
grep -i Uploading /girish/sftp3.log >/girish/output3
grep -i transfer /girish/sftp4.log >/girish/output4

var1=`cmp /anshu/$file /amit/$file`
echo $var1
var2=`cmp /girish/f2 /girish/f3` # value in f2 and f3 are identical which is a
echo $var2

if [ $var1 = $var2 ] 
then 
break
fi

else
sleep 60
fi

done 

done

Last edited by vino; 03-12-2008 at 01:49 AM. Reason: code tags
Reply With Quote
Forum Sponsor
  #2  
Old 03-12-2008
Registered User
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 352
This might help...

basename=${0##*/}
trap 'print "$basename: Unexpected rc $? at line $LINENO"; exit 1' ERR # exits script for any non-zero return-code
trap '
#
# command to clean up on any script exit, any rc value
#
' EXIT
Reply With Quote
  #3  
Old 03-25-2008
Registered User
 

Join Date: Sep 2007
Location: Poland
Posts: 63
I am not sure if I understood this correctly: You want to initialize SFTP transfer and detect when it fails or is running more than 5-10 minutes?
If so, then you could use multiple threads where one is watching the other one, or you could just wait until the SFTP session finishes.

Example code (I give no guarantee that it will work):
#!/bin/ksh
sftp -b batch_script login@server &
sleep 600
if [[ -n $(jobs) ]] ;then
typeset mypsid=$(jobs -l|awk '{print $3}')
if [[ -n "$mypsid" ]] && [[ "$mypsid" == [0-9]+ ]] ;then
kill -9 $mypsid
fi
repeat_sftp_sending
fi
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:31 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0