Operating System - HP-UX
1846763 Members
5731 Online
110256 Solutions
New Discussion

Question about the trap command ..

 
SOLVED
Go to solution
someone_4
Honored Contributor

Question about the trap command ..

Hey everone ,
I just got done reading some information about the trap command. One part that caught my attention is that traps are useful if you want to capture an even in your sytem. Setting a trap on a particular signal for example can inform you about a system event via email. Can someone please provide me with examples on how the trap command would be usefull?

Thanks
Richard
11 REPLIES 11
Sridhar Bhaskarla
Honored Contributor

Re: Question about the trap command ..

There are two kinds of traps.. Snmp trap and the 'trap' command. I assume you are talking about trap command.

Trap is used to trap the signals. Depending on the signal, we can ask trap to do whatever we want.

Check this script.

printout ()
{
echo "ola..ola..ola"
}

trap `printout` INT TERM
sleep 180

Now run this script and try to press ctrl-c. (sending Signal TERM) It is going to run the function printout and echos the message out. So you can embed any code there in the function. Just an example.

I hope you got it.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
G. Vrijhoeven
Honored Contributor

Re: Question about the trap command ..

Richard,

The trap command can be used to put processes to sleep in and give you an shell, give status information etc.

Hope this gives you some ideas of your own

Gideon

A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Question about the trap command ..

Hi Richard:

Perhaps this little example will give you a better understanding.

#!/usr/bin/sh

PID=${$}

trap 'eval echo "Trap Test ${PID}"' 1 2

echo "pid = ${PID}"
CH="x"
while [ "${CH}" != "q" ]
do
echo "Enter q to quit: \c"
read CH
echo "Out : ${CH}"
done
exit 0

If you enter a Ctrl-C (usually intr is set to Ctrl-C) then you will see the trap mesaage; you can also go to another session and send a
kill -2 (or a kill -1) PID using the PID displayed to do the same thing.

man sh_posix and look for the 'trap' section for more details.

Clay
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Question about the trap command ..

Hi Richard:

Consider an easy way in a script to construct an epilog procedure -- one that executes automatically *anytime* you exit *somewhere* within your code:

#!/usr/bin/sh
F=/tmp/$$.sh
trap 'echo exiting!;rm $F' EXIT
...

...Now, whenever you 'exit' this script, the file "F" will be removed after the announcement that your script is "exiting!".

Another useful case is to send your script a signal, with the signal USR1 or USR2.

#/usr/bin/sh
trap 'echo "I am alive!"' USR1
...

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Question about the trap command ..

Hi Richard:

Interestingly there is another question on 'trap's in this thread that you will find informative, too:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,11866,0x2b01c7af36b7d5118ff10090279cd0f9,00.html

Regards!

...JRF...
someone_4
Honored Contributor

Re: Question about the trap command ..

Hey James
Your first exapmle was great.

But what is

#/usr/bin/sh
trap 'echo "I am alive!"' USR1

supposed to do ??

Richard
A. Clay Stephenson
Acclaimed Contributor

Re: Question about the trap command ..

Hi Richard:

In that example, suppose you had some script running that had the trap statement in it.
Let's suppose that the Process ID (pid) of that process was 3009.

From another shell you could:
kill -s USR1 3009
or kill -16 3009 (USR1 is signal number 16)
and the original process would then echo "I am alive" to stdout. It is a mechanism for a script to respond to an asynchronous event.

If you really want to understand what's going on 'under the hood'; man signal(2) and kill(2)
to see how the signal handling is done within
the shell.

Clay
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Question about the trap command ..

Hi Richard:

Have a look at 'man 5 signal'. You will see that there are two "free" signals for communication -- SIGUSR1 and SIGUSR2. The interpretation or use of them is up to the developer of the process. I choose one of them in my example to underscore a technique.

The actual example I gave would simply echo "I am alive!" to stdout any time the process trapped the SIGUSR1 signal (kill -s USR1 ). It isn't likely that you would do this. Rather, you might use it to log a message (perhaps of progress or other state) in a logfile, etc.

If you were merely interested in testing if a process was running, you can issue a 'kill -0 '. This is useful to test if a process still exists. A return code of zero (0) indicates it does. A non-zero return code signifies an invalid pid.

Regards!

...JRF...
someone_4
Honored Contributor

Re: Question about the trap command ..

Ok
I hate to beat this trap issue to death but I understand now but I have some questions.
I have trap6.sh
#/usr/bin/sh
trap 'echo I am alive!' 1 2
sleep 120

if I do
#./trap6
And then ctrl-c the what is a signal 2.
I get "I am alive".
But When I do
#nohup ./trap6 &
then
#kill -2 PID OF trap6
I get nothing back. I thought I would get back
"I am alive". Why does this not work with a kill -2 ?

I understand how this is helpfull and understand the concept and idea of traps. They actully "trap" an action untill you get a certain sigal from the list of kill -l . I do the command
#trap
you see the traps that are set up for your shell. Like
trap -- 'echo '\''logout root'\' EXIT
So when you send an EXIT signal you get
"logout root"
Or if I add a trap
#trap pwd DEBUG
the pwd is executed after every command.


A. Clay Stephenson
Acclaimed Contributor

Re: Question about the trap command ..

Hi Richard:

This is exactly why I wanted you to read the signal(2) man pages. You can instruct a process to ignore a signal. Nohup does just that: The child process will ignore a SIGHUP (hangup - 1) and a SIGINT (interrupt - 2 - generated by the intr char normally a Ctrl-C.).

You can also use a trap statement to ignore a signal:

trap '' 2
instructs your script to ignore signal 2 (interrupt).

If it ain't broke, I can fix that.