Re: killing child processes
From: Chuck (chuckh_at_softhome.net)
Date: 11/03/03
- Next message: rramson: "copying file to a new name."
- Previous message: Stephane CHAZELAS: "Re: sed applying the same regexp twice"
- In reply to: John DuBois: "Re: killing child processes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 3 Nov 2003 13:39:48 GMT
spcecdt@deeptht.armory.com (John DuBois) wrote in
news:3fa2dc76$0$1097$8eec23a@newsreader.tycho.net:
> In article <Xns94258FCEC481Cchuckhsofthomenet@130.133.1.4>,
> Chuck <chuckh@softhome.net> wrote:
>>Bill Marcum <bmarcum@iglou.com> wrote in
>>news:mva971-qik.ln1@don.localnet:
>>
>>> On 30 Oct 2003 20:40:39 GMT, Chuck
>>> <chuckh@softhome.net> wrote:
>>>> How can I get one child process to kill another child process in
>>>> ksh? What I am tying to do is launch two child processes from a
>>>> script. Whichever one finishes first, I want it to kill the other
>>>> one. I have tried using the kill %jobno syntax but that doesn't
>>>> seem to work inside a shell script. When the script exits, the
>>>> child process that I supposedly killed still shows up when I do ps
>>>> -f. Here's a very basic example of what I want to do.
>>>>
>>>
>>>> I have tried using capturing the PID of the background processes
>>>> into variables right after launching them, and using the variables
>>>> in place of %1 or %2 but that does the same thing.
>>>
>>> You can't use variables, because a child process can only inherit
>>> copies of variables that existed before the child started. You can
>>> write the PIDs into files and use them.
>>>
>>
>>The problem is not that the child processes don't know which pids to
>>kill. They do. It's that the kill command does nothing.
>>
>>Example:
>>
>>function timer {
>> sleep 60
>> echo done
>>}
>>$ timer &
>>[1] 4908
>>$ echo $!
>>4908
>>$ ps -f
>> UID PID PPID TTY STIME COMMAND
>> isscha 5044 1 0 14:01:06 /usr/bin/pdksh
>> isscha 4908 5044 0 14:03:49 /usr/bin/pdksh
>> isscha 5224 4908 0 14:03:49 /usr/bin/sleep
>> isscha 5080 5044 0 14:03:56 /usr/bin/ps
>>$ kill 4908
>>
>>At this point 4908 should be gone, but it's not !!!
>>
>>$ ps -f
>> UID PID PPID TTY STIME COMMAND
>> isscha 5044 1 0 14:01:06 /usr/bin/pdksh
>> isscha 4908 5044 0 14:03:49 /usr/bin/pdksh
>> isscha 5224 4908 0 14:03:49 /usr/bin/sleep
>> isscha 4928 5044 0 14:04:05 /usr/bin/ps
>>$
>>
>>A similar thing happens if I say kill %1. If run from a shell script,
>>it kills the psksh shell, but not the sleep command that is it's
>>child.
>
> The above may be a pdksh bug - it works in ksh. But even if it
> worked, it still wouldn't help. %jobno works for jobs that are
> children of the current shell. Neither of your background jobs will
> be a child of the other.
>
> I do something like this, in ksh88:
>
> realjob&
> realjob_pid=$!
> (
> (
> sleep 240&
> echo $!
> wait $! && {
> some_logger "Killing hung realjob (pid=$realjob_pid)"
> kill -9 $realjob_pid
> }
> ) &
> ) | read timer_pid
> wait
> kill $timer_pid
>
>
> The timer is backgrounded from a subshell, so 'wait' won't wait for
> it. The sleep process' pid is echoed so we can kill it if realjob
> exits first. If the timer's wait indicates a normal exit (sleep
> expired), it kills realjob & does some logging; if not, it means the
> sleep was killed off so the timer just exits.
>
> John
Works great. All I did was add a change ") &" to ) > /dev/null &" to
suppress the "$PID: terminated" message. Thanks for your help.
- Next message: rramson: "copying file to a new name."
- Previous message: Stephane CHAZELAS: "Re: sed applying the same regexp twice"
- In reply to: John DuBois: "Re: killing child processes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|