- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Question about the trap command ..
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2001 09:27 AM
10-08-2001 09:27 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2001 09:41 AM
10-08-2001 09:41 AM
Re: Question about the 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2001 09:48 AM
10-08-2001 09:48 AM
Re: Question about the trap command ..
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2001 10:16 AM
10-08-2001 10:16 AM
SolutionPerhaps 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2001 10:19 AM
10-08-2001 10:19 AM
Re: Question about the trap command ..
Check this out to know about using trap in your script.
http://docs.hp.com/cgi-bin/fsearch/framedisplay?top=/hpux/onlinedocs/B2355-90680/B2355-90680_top.html&con=/hpux/onlinedocs/B2355-90680/00/03/316-con.html&toc=/hpux/onlinedocs/B2355-90680/00/03/316-toc.html&searchterms=trap&queryid=20011008-111935
http://docs.hp.com/cgi-bin/fsearch/framedisplay?top=/hpux/onlinedocs/B2355-90680/B2355-90680_top.html&con=/hpux/onlinedocs/B2355-90680/00/03/315-con.html&toc=/hpux/onlinedocs/B2355-90680/00/03/315-toc.html&searchterms=trap&queryid=20011008-111740
Hope this helps.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2001 10:20 AM
10-08-2001 10:20 AM
Re: Question about the trap command ..
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2001 12:28 PM
10-09-2001 12:28 PM
Re: Question about the trap command ..
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2001 07:14 AM
10-11-2001 07:14 AM
Re: Question about the trap command ..
Your first exapmle was great.
But what is
#/usr/bin/sh
trap 'echo "I am alive!"' USR1
supposed to do ??
Richard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2001 07:28 AM
10-11-2001 07:28 AM
Re: Question about the trap command ..
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2001 08:11 AM
10-11-2001 08:11 AM
Re: Question about the trap command ..
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
If you were merely interested in testing if a process was running, you can issue a 'kill -0
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2001 10:55 AM
10-11-2001 10:55 AM
Re: Question about the trap command ..
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2001 11:02 AM
10-11-2001 11:02 AM
Re: Question about the trap command ..
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).