Sponsored Content
Top Forums UNIX for Advanced & Expert Users how to use trap command in shell script Post 302174470 by girish.batra on Tuesday 11th of March 2008 07:23:07 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
SFTP-SERVER(8)						    BSD System Manager's Manual 					    SFTP-SERVER(8)

NAME
sftp-server -- SFTP server subsystem SYNOPSIS
sftp-server [-ehR] [-f log_facility] [-l log_level] [-u umask] DESCRIPTION
sftp-server is a program that speaks the server side of SFTP protocol to stdout and expects client requests from stdin. sftp-server is not intended to be called directly, but from sshd(8) using the Subsystem option. Command-line flags to sftp-server should be specified in the Subsystem declaration. See sshd_config(5) for more information. Valid options are: -e Causes sftp-server to print logging information to stderr instead of syslog for debugging. -f log_facility Specifies the facility code that is used when logging messages from sftp-server. The possible values are: DAEMON, USER, AUTH, LOCAL0, LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5, LOCAL6, LOCAL7. The default is AUTH. -h Displays sftp-server usage information. -l log_level Specifies which messages will be logged by sftp-server. The possible values are: QUIET, FATAL, ERROR, INFO, VERBOSE, DEBUG, DEBUG1, DEBUG2, and DEBUG3. INFO and VERBOSE log transactions that sftp-server performs on behalf of the client. DEBUG and DEBUG1 are equivalent. DEBUG2 and DEBUG3 each specify higher levels of debugging output. The default is ERROR. -R Places this instance of sftp-server into a read-only mode. Attempts to open files for writing, as well as other operations that change the state of the filesystem, will be denied. -u umask Sets an explicit umask(2) to be applied to newly-created files and directories, instead of the user's default mask. For logging to work, sftp-server must be able to access /dev/log. Use of sftp-server in a chroot configuration therefore requires that syslogd(8) establish a logging socket inside the chroot directory. SEE ALSO
sftp(1), ssh(1), sshd_config(5), sshd(8) T. Ylonen and S. Lehtinen, SSH File Transfer Protocol, draft-ietf-secsh-filexfer-00.txt, January 2001, work in progress material. HISTORY
sftp-server first appeared in OpenBSD 2.8. AUTHORS
Markus Friedl <markus@openbsd.org> BSD
January 9, 2010 BSD
All times are GMT -4. The time now is 03:50 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy