trap - advanced usage

Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers trap - advanced usage
# 1  
Old 05-30-2002
Question trap - advanced usage

Alright gang, I've done pretty exhaustive search on effective uses of the 'trap' command - and have found that everybody out there uses the same example...

trap "rm -f $tmp; exit" 0 1 2 15

...and that doesn't do a whole lot for me.

I'm looking to ignore all 1's and trap, notify, and exit 1 on anything else - OR the 2nd instance of a 1 signal.

I've tried:

trap '' 1

(where TRAPPER is a sub-routine that sends a message then exits)

but the script keeps looping eventualy overunning my buffer and giving me all kinds of grief - so any suggestions? Then goal in all of this is to come up with a *really* good error handler for unix scripts that functions similar to:

VBA's "on ERR goto ERROR_HANDLER" and
SQL's "whenever sqlerror exit 1"



Ursa Major
# 2  
Old 05-30-2002
First, the ERR trap seems to work in a straight forward manner. I tried:
#! /usr/bin/ksh
TRAPPER() { echo trapper ran ; }
exit 0

And I got pretty much what I expected. This is the first time I have ever trapped on ERR though. So it's not like I have a lot of experience with it.

The second part of your question has raised an interesting point. Here is a script that exits after it gets two signal 15's...
#! /usr/bin/ksh
TRAPPER() { echo TRAPPER ran ; ((c=c+1)) ; ((c==2)) && exit 0 ; }
trap TRAPPER 15
echo my pid is $$
while : do
      sleep 5
      echo still here
exit 0

And again this script works just as I would expect. From another window, I just send a couple of signal 15 to it and it dies on the second one. But when I try this with signal 1, I fail! And I'm not sure why. If looks like ksh is treating signal 1 specially. I'm not sure why this is. It may be a bug.
# 3  
Old 05-30-2002

Cool - thanks. One quick question - exactly how is it that you 'send' the 15 signal to the executing script?
# 4  
Old 05-30-2002
I send the signal via kill:
kill -15 pid
where pid is the value displayed by the script in the other window.
# 5  
Old 05-30-2002

Smilie, duh! Thanks!

I've also been given a number of other options which I'm going to munge together and see what happens - I'm betting I'll have more questions - this 'trap' doesn't seem all that friendly/useful if you ask me...(which you didn't, but...Smilie

Off to unixland...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Memory usage per user,percent usage,sytem time in ksh

Let's say i have 20 users logged on Server. How can I know how much memory percent used each of them is using with system time in each user? (2 Replies)
Discussion started by: roy1912
2 Replies

2. Homework & Coursework Questions

VM trap may work differently than a pure install trap.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: That is the last reply I received from my instructor, and I'm looking for some alternatives. When using... (2 Replies)
Discussion started by: newuser45
2 Replies

3. UNIX for Dummies Questions & Answers

Command to display the space usage (memory usage) of a specific directory.

Hi all, Can you please tell me the command, with which one can know the amount of space a specific directory has used. df -k . ---> Displays, the amount of space allocated, and used for a directory. du -k <dir name> - gives me the memory used of all the files inside <dir> But i... (2 Replies)
Discussion started by: abhisheksunkari
2 Replies

4. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

5. Shell Programming and Scripting

Printing to PDF - Advanced usage

Hello, Just a rather long query regarding printing to PDF. I have installed the Cups-PDF printing system which enables me to send a normal text file and create a PDF of it. I then use the pdftk to merge that file with a template. My question is, that I have a database system that will need... (2 Replies)
Discussion started by: stuaz
2 Replies

6. Solaris

current CPU usage, memory usage, disk I/O oid(snmp)

Hi, I want to monitor the current cpu usage, monitor usage , disk I/o and network utlization for solaris using SNMP. I want the oids for above tasks. can you please tell me that Thank you (2 Replies)
Discussion started by: S_venkatesh
2 Replies

7. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

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

9. Shell Programming and Scripting

Building a better mouse trap, or How many lines of code does it take to trap a mouse?

Hello all, I'm hoping to get a little insight from some of the wily veterans amongst you. I've written a script to check for new outgoing files to our vendors located on our ssl server. It seems to be working ok, but the final question here, will be one of logic, and/or a better way to... (4 Replies)
Discussion started by: mph
4 Replies

10. Programming

Monitor CPU usage and Memory Usage

how can i monitor usages of CPU, Memory, Hard disk etc. under SUN Solaries through a c program or java program i want to store that data into database so i can show it graphically thanks in advance (2 Replies)
Discussion started by: Gajanad Bihani
2 Replies
Login or Register to Ask a Question