Help not trigger the trap


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help not trigger the trap
# 1  
Old 09-26-2014
Help not trigger the trap

Code:
mainpid=$$
(trap "echo timeout" SIGTERM SIGKILL SIGINT; sleep 5; kill $mainpid) &
watchdogpid=$!

sleep 10 #do something 
kill $watchdogpid

I design a timeout function script
I don't see the echo "timeout" is on the screen
# 2  
Old 09-26-2014
Quote:
Originally Posted by yanglei_fage
Code:
mainpid=$$
(trap "echo timeout" SIGTERM SIGKILL SIGINT; sleep 5; kill $mainpid) &
watchdogpid=$!

sleep 10 #do something 
kill $watchdogpid

I design a timeout function script
I don't see the echo "timeout" is on the screen
Why would you expect for this code to ever print timeout?
Let's look at the likely sequence of operations:
Code:
parent: mainpid=PPID
parent: watchdogpid=CPID
parent: sleep 10
child: trap ...
child: sleep 5
child: kill $mainpid
child: exit
parent: killed by SIGTERM signal from child

so the child never received a signal that was caught by the trap command.

But, if you change the sleep 10 to sleep 1 then the sequence of operations is something like:
Code:
parent: manpid=PPID
parent: watchdogpid=CPID
parent: sleep 1
child: trap 'echo timeout' SIGTERM SIGKILL SIGINT
child: sleep 5
parent: kill $watchdogpid SIGTERM
child: sleep 5 terminated by SIGTERM signal from parent
child: echo timeout
child: kill $mainpid
child: exit
parent: exit (or terminate from SIGTERM from child

In this case, you see the output from the echo when the parent sends the child a SIGTERM signal.

Note that you can't successfully catch a SIGKILL. Did you perhaps mean to have this code instead:
Code:
mainpid=$$
trap "echo timeout; exit 1" SIGTERM SIGINT
(sleep 5; kill $mainpid) &
watchdogpid=$!

sleep 10 #do something 
kill $watchdogpid

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To trigger script on particular instance

Hi Guys, I have a main_script.sh which runs every day and scheduled in crontab. in the main script i read data from config file test.config apple mango orange main_script.sh for i in `cat test.config` do if then echo 'Apple' (3 Replies)
Discussion started by: Master_Mind
3 Replies

2. Shell Programming and Scripting

Automatic script trigger

Hi, I'm looking for a way to solve the following scenario: A shell should automatically trigger / run when a text file is placed or present at a specific location. My idea - to create a cron / anacron for every minute and inside that i will call a temp script. Temp script will move to my... (9 Replies)
Discussion started by: Gautham
9 Replies

3. OS X (Apple)

AC to DC trigger pulse for AudioScope.sh.

Hi all... Has _below_ ever been done in UNIX shell scripting before? (I have done this easily in Python but this is using purely the shell.) The DEMO version IS built and has been tested. Pre-amble... I now need at least one control pulse for the AudioScope.sh when in PURELY audio I/O mode,... (2 Replies)
Discussion started by: wisecracker
2 Replies

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

5. Shell Programming and Scripting

Trigger functionality in Unix

Hi, I want a script , which searches the log for the term/phrase "JFSnapshotService::systemSnapshot: Starting data capture. This may take awhile depending upon system workload." and if there is some logging like this, it has to mail me that this data capture process is happening., Below is... (2 Replies)
Discussion started by: cratercrabs
2 Replies

6. UNIX and Linux Applications

update trigger

hi all, i hope i am posting this /beginner) question in the right forum: i want to create an update trigger, which rolls back a transaction if a record of a table is updated. the table has - amongst others - a field 'statusid' - if a record in this table has the statusid X and it is attempted... (0 Replies)
Discussion started by: kalinkula
0 Replies

7. Shell Programming and Scripting

Trigger with condition

If test.ksh is successful then I have a sequence of script which needs to execute automatically. Is it possible to capture the return code to execute the next script automatically? what is better way of doing this. (4 Replies)
Discussion started by: zooby
4 Replies

8. Shell Programming and Scripting

Trigger Enter

Hello, I need to trigger every time enter has been clicked while some one on terminal i tried to googleit but with out result any idea ?? thanks in advance (3 Replies)
Discussion started by: AYAK
3 Replies

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

10. 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
Login or Register to Ask a Question