Saturday, July 21, 2012

Tutorial on TRAP in Unix


Tutorial on TRAP in Unix

Some times in Unix we want to prevent our script from abnormal conditions and interrupts, this can be handled using the trap command in Unix.When you press the Ctrl+C or Break key at your terminal during execution of a shell program, normally that program is immediately terminated, and your command prompt returned. This may not always be desirable. For instance, you may end up leaving a bunch of temporary files that won’t get cleaned up.
Trapping these signals is quite easy, and the trap command has the following syntax:
Format: trap command signal
Trap enables signals handling ,signal are sent to running process to indicate that an event (external to the process) has occurred that the process must respond,Now, trap catches a signal sent to the process, and take action on the signal using the action defined in the trap command, Instead of letting the signal take it’s default effect on the process.Command would be one or more command that will be executed (separated by colon) when any of specified signal received.
Signal: The signal we want to trap , we can specify more than one signal here, it can be both numeric or string (signal name) format as described below.
0 EXIT Exit
1 HUP Hang-up
2 INT Interrupt (like ctrl +c)
15 TERM Software termination signal
While scripting we might get confuse where we put our trap command, trap command executed whenever one of the traps on its list occurs,This means that our shell needs to know in advance what to do about a signal before a signal occurs.This advice that we have our trap command before any otherexecutable command, In this case the shell will be able to know which signal to send our trap command.
Like : trap 1………
Command 1
Command 2
Command 3
trap 2 ………………
Command 4
Command 5
Command 6
Here, trap 1 will be executed for command 1,2,3, and trap 2 will be executed for command 4,5,6.
Now we can define multiple traps in our program 
Usage of trap : Suppose we want to prevent our program from hang-up and interrupt signals.trap ‘echo “you program interrupted”;rm /tmp/daemons1.flg;exit 1’ 1 2 .Here it will echo the msg, remove some log or flag file according to our requirement and then exit, once it receive 1 or 2 signal,Sometimes we might want to ignore certain signals while performing some operation that we don’t want interrupted,
trap ‘ ‘ 1 2
Here we are ignoring 1 and 2 signal by providing empty command in trap.We can also defined function in trap, if we have much command to execute,trap ‘function_name;exit1’ INT HUP
Change the trap result back for signals We can change the default action of a signal, like trap 1 2
NOTE : If you ignore a signal, all subshells also ignore that signal. However, if you specify an action to be taken on receipt of a signal, all subshells will still take the default action on receipt of that signal.The commands specified to trap must be enclosed in quotes if they contain more than one command. Also note that the shell scans the command line at the time that the trap command gets executed and also again when one of the listed signals is received.

No comments:

Post a Comment