how to use trap command in shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users how to use trap command in shell script
# 1  
Old 03-11-2008
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 05:49 AM.. Reason: code tags
# 2  
Old 03-12-2008
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
# 3  
Old 03-25-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to find the wrong filename in a path and raise a trap for it

Example: I have server name A with an IP : 125.252.235.455 I have an username /password to login into this server under SSH connection In this server i have a path /apps/user/filename(Big.txt) Everyday we used to get the filename as Big.txt. I want a shell script to monitor this path... (4 Replies)
Discussion started by: ChandruBala73
4 Replies

2. Shell Programming and Scripting

Shell script to set trap for finding cron job failures

Unix box: solaris 5.8 Server: IP Need to to set trap for cron job failures by writing a shell script (5 Replies)
Discussion started by: ChandruBala73
5 Replies

3. Shell Programming and Scripting

Trap Oracle error in shell script

sqlplus -s usrname/password@dbSID <<-SQL >> logfile @create_table.sql commit; quit; SQL I am running this script to execute an sql file. I want to display the oracle error if anything found during execution of the sql file and exit from script. Please suggest How do it. (1 Reply)
Discussion started by: millan
1 Replies

4. UNIX for Dummies Questions & Answers

trap command

I'm learning about the trap command from my bash book. I tried out the little script they gave: trap "echo 'You hit control-C!' " INT while true; do sleep 60 done But when I type control-c, the script just stops and the message is not displayed. I checked stty all and saw that control-c... (11 Replies)
Discussion started by: Straitsfan
11 Replies

5. Shell Programming and Scripting

How trap a signal in shell script?

Hi , i have a scenario where...i have to put a check where if script is executing more than 15mins i have to kill that script and n retry again 2nd time. i this case i can use background process to do it but i feel trap will be the efficent way to do so... but i dont know much about it... (1 Reply)
Discussion started by: crackthehit007
1 Replies

6. Shell Programming and Scripting

shell scripting best practices - trap command

I know there is a command called trap which can be used to capture the signals from a shell script and redirect the control to a required function (such as a cleanup). My question is - Those of you who have written lot of shell scripts - do you always write a set of trap commands to capture... (4 Replies)
Discussion started by: sagar_evc
4 Replies

7. Shell Programming and Scripting

Nullify the effect of Trap command in later part of the script

Hi All, i have an issue regarding trap command. i have specified trap function in the beginning of the script to catch some signals but in the later part of the script i want to remove the effect of this. Can anybody help me out of this. for e.g. pressing Ctrl+C for the first time should... (2 Replies)
Discussion started by: vikas_kesarwani
2 Replies

8. UNIX for Advanced & Expert Users

trap ctrl c in shell script

how to trap the ctrl c in unix shell script my script is running in while loop it should not be terminate with ctrl c. if i press ctrl c while running script it shloud ignore the same. please healp.......... thanks in advance (2 Replies)
Discussion started by: arvindng
2 Replies

9. Shell Programming and Scripting

Cntl+z Trap is not detecting ??? Help required to add a trap detection ???

Hi folks, I have tried to add some trap detection in the below script....this script is used to monitor database activities...in a rather awkward way :rolleyes:.... The idea behind adding trap is that....this script creates lots of temporary files in the running folder to store the count... (1 Reply)
Discussion started by: frozensmilz
1 Replies

10. Shell Programming and Scripting

Use of TRAP Command

Hi, I would like to know the use of TRAP command. I am very new to the UNIX environment. I have just started learning the basic. So please teach me in a very simple way to understand. Also i would like to know the use of following command: trap 'dialog --msgbox "Script Aborted1" 6 50 ;... (2 Replies)
Discussion started by: Deepakh
2 Replies
Login or Register to Ask a Question