trap not work in program with a subshell environment
From: Michael Wang (mwang@unixlabplus.com)
Date: 04/24/03
- Next message: Kevin Rodgers: "Re: question regarding removing files under /usr/spool/cron/atjobs"
- Previous message: Barry Margolin: "Re: test: argument expected error"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
From: mwang@unixlabplus.com (Michael Wang) Date: Wed, 23 Apr 2003 22:26:27 GMT
Here is my sample program:
--
#!/usr/dt/bin/dtksh
{
{
{
(
PID= unset PID; (( PID = 0 ))
function handle_signal {
(( PID == 0 )) || {
kill $PID
print "killed $PID."
}
print "inside trap."
exit 1
}
trap handle_signal INT TERM
/bin/sleep 1200 &
PID=$!
wait $PID
)
print "exit_code=$?"
} 9>&- | tee -a /dev/null >&9
} 2>&1 | tee -a /dev/null >&2
} 9>&1
print "I am here."
--
And here is the process tree. Please note the program
"1.ksh" appears twice in the process tree.
7204 /usr/dt/bin/dtksh ./1.ksh
7206 tee -a /dev/null
7205 tee -a /dev/null
7207 /usr/dt/bin/dtksh ./1.ksh
7208 /bin/sleep 1200
If I kill the second "1.ksh" (PID: 7207), then I get the intended
behavior. However if I kill the first "1.ksh" (PID: 7204), then
the program terminates without trapping.
And this is the problem, because the first process is the process that
will be killed by the parent process, which does not know the second
process.
If I move the function handle_signal, and the trap outside the subshell
environment (...):
--
#!/usr/dt/bin/dtksh
PID= unset PID; (( PID = 0 ))
function handle_signal {
(( PID == 0 )) || {
kill $PID
print "killed $PID."
}
print "inside trap."
exit 1
}
trap handle_signal INT TERM
{
{
{
(
/bin/sleep 1200 &
PID=$!
wait $PID
)
print "exit_code=$?"
} 9>&- | tee -a /dev/null >&9
} 2>&1 | tee -a /dev/null >&2
} 9>&1
print "I am here."
--
and I got a process tree:
8947 /usr/dt/bin/dtksh ./2.ksh
8950 tee -a /dev/null
8948 tee -a /dev/null
8949 /usr/dt/bin/dtksh ./2.ksh
8951 /bin/sleep 1200
The problem is no matter which process (8947 or 8949) I kill,
the program does not terminate.
However if I get rid of the complicated subshell environment:
--
#!/usr/dt/bin/dtksh
PID= unset PID; (( PID = 0 ))
function handle_signal {
(( PID == 0 )) || {
kill $PID
print "killed $PID."
}
print "inside trap."
exit 1
}
trap handle_signal INT TERM
/bin/sleep 1200 &
PID=$!
wait $PID
print "I am here."
--
I get a process tree shown below. Please note
the process named "3.ksh" appear only once.
9417 /usr/dt/bin/dtksh ./3.ksh
9418 /bin/sleep 1200
And if I kill the process, it gives the expected behaviour.
But the problem is I need the complicated subshell environment
so that stdout and stderr are duplicated to a file. And my goal
is with this subshell environment, trap is used when I kill the
first process, and ideally trap is also used when I kill the
second process, or any process in the tree, as I can not predict
how user uses "ps", "grep", and "kill".
Thanks.
--
Michael Wang * http://www.unixlabplus.com/ * mwang@unixlabplus.com
- Next message: Kevin Rodgers: "Re: question regarding removing files under /usr/spool/cron/atjobs"
- Previous message: Barry Margolin: "Re: test: argument expected error"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|
|