Re: killing child processes

From: Chuck (chuckh_at_softhome.net)
Date: 11/03/03


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.



Relevant Pages

  • Re: [opensuse] Help Killling Process
    ... will not die nor will it display anything. ... I remember from the 'old days' that there is a cli command to ... identify a process id and another one to kill a process. ... This will give you a listing of the processes and their pid' although ...
    (SuSE)
  • Re: openoffice.org wont open files on my usb drive
    ... First check whether openoffice is running/not by the following command ... Here you get the process id (pid) of open office ... Kill the process by the following command ...
    (Debian-User)
  • Re: [opensuse] Help Killling Process
    ... I remember from the 'old days' that there is a cli command to ... (the first number for each process is the PID, the second is the PID of the ... Locate the PID for the process you wish to terminate and use the 'kill' ... To kill the xterm process in the above example, ...
    (SuSE)
  • Re: Question about a dd command.
    ... elmo wrote: ... basic 'dd' command that will copy and display the transfer action. ... variable named "pid" so we can use it later to send signals to the process. ... kill $pid: Kill the actual process. ...
    (Ubuntu)
  • Re: A Linux timed audio recorder
    ... sleep $X # wait for some time ... kill $Pid # hoping that noone re-used that PID in the meantime... ... So here a nice example that uses a longer script then just one line: ...
    (alt.os.linux.suse)