Bourne Shell script, that kills its child-processes?

Bourne Shell script, that kills its child-processes?

Post by Juhapekka Tolvane » Wed, 26 Jul 2000 04:00:00



Do you know, how I can create Bourne Shell script, that kills every
child-process, that it has started as background processes. Now it is
done like this:

trap '
   killall -15 xscreensaver-command 2> /dev/null
   echo "0" > $WATCHER_FILE
   exit 0
' 1 2 3 15 20

set +o noclobber
echo "0" > $WATCHER_FILE

xscreensaver-command -watch |
while read line
do
  foo=$(echo $line | awk '{print $1}' - )
  if test "$foo" = "BLANK"
  then
    my_debug "screen is blanked"
    echo "1" > $WATCHER_FILE
  elif test "$foo" = "LOCK"
  then
    my_debug "screen is locked"
    echo "1" > $WATCHER_FILE
  elif test "$foo" = "UNBLANK"
  then
    my_debug "screen is UNblanked"
    echo "0" > $WATCHER_FILE
  fi
done

That "killall -15 xscreensaver-command" is not very elegant. I want to
kill only that one xscreensaver-command -process and child-process,
that is created in that line saying "while read line".

--

http://www.cc.jyu.fi/~juhtolv/ * * "STRAIGHT BUT NOT NARROW !!"
---------------------------------------------------------------
"if i was twice the man i could be, i'd still be half of what
you need"                                       Nine Inch Nails

 
 
 

Bourne Shell script, that kills its child-processes?

Post by Dan Merc » Wed, 26 Jul 2000 04:00:00




> Do you know, how I can create Bourne Shell script, that kills every
> child-process, that it has started as background processes. Now it is
> done like this:

   1. You are not running a Bourne shell.  You are running a posix
      shell

   2. Useless use of awk:

      xscreensaver-command -watch | while read foo waste

   3. Useless use of test:
      do
      case $foo in
         BLANK) ...;;
         LOCK) ...;;
         UNBLANK) ...;;
         *)
      esac

   4. You are not running any background processes.  xscreensaver-command
      will not survive the script.

--
Dan Mercer

Quote:> trap '
>    killall -15 xscreensaver-command 2> /dev/null
>    echo "0" > $WATCHER_FILE
>    exit 0
> ' 1 2 3 15 20

> set +o noclobber
> echo "0" > $WATCHER_FILE

> xscreensaver-command -watch |
> while read line
> do
>   foo=$(echo $line | awk '{print $1}' - )
>   if test "$foo" = "BLANK"
>   then
>     my_debug "screen is blanked"
>     echo "1" > $WATCHER_FILE
>   elif test "$foo" = "LOCK"
>   then
>     my_debug "screen is locked"
>     echo "1" > $WATCHER_FILE
>   elif test "$foo" = "UNBLANK"
>   then
>     my_debug "screen is UNblanked"
>     echo "0" > $WATCHER_FILE
>   fi
> done

> That "killall -15 xscreensaver-command" is not very elegant. I want to
> kill only that one xscreensaver-command -process and child-process,
> that is created in that line saying "while read line".


 
 
 

Bourne Shell script, that kills its child-processes?

Post by Juhapekka Tolvane » Wed, 26 Jul 2000 04:00:00





> > Do you know, how I can create Bourne Shell script, that kills every
> > child-process, that it has started as background processes. Now it is
> > done like this:

>    1. You are not running a Bourne shell.  You are running a posix
>       shell

I am running ash, to be exact. AFAIK it can do everything, that
original Bourne Shell can do and everything that POSIX requires and
not very much in addition.

Quote:>    2. Useless use of awk:

>       xscreensaver-command -watch | while read foo waste

>    3. Useless use of test:
>       do
>       case $foo in
>          BLANK) ...;;
>          LOCK) ...;;
>          UNBLANK) ...;;
>          *)
>       esac

Thanks. Fixed.

Quote:>    4. You are not running any background processes.
>    xscreensaver-command will not survive the script.


% ps xf                                                            6672 | pts/4
  PID TTY      STAT   TIME COMMAND
(Clip)
 3300 ?        S      0:00 ash /home/juhtolv/textures/watch.sh
 3303 ?        S      0:00  \_ xscreensaver-command -watch
 3304 ?        S      0:00  \_ ash /home/juhtolv/textures/watch.sh
(Clip)

Isn't that PID 3304 background process?

 * * *

Well, what if I really have many child processes as background
processes?  How can I kill them all, when my script exits? It is
possible to put PID of that process to environment variable, like
this:

$WATCHER &
WATCHER_PID="$!"

But what if I have two or more of them? Should I put those PIDs to
array?  What POSIX says about arrays?

--

http://www.cc.jyu.fi/~juhtolv/ * * "STRAIGHT BUT NOT NARROW !!"
---------------------------------------------------------------
"if i was twice the man i could be, i'd still be half of what
you need"                                       Nine Inch Nails

 
 
 

Bourne Shell script, that kills its child-processes?

Post by Dan Merc » Wed, 26 Jul 2000 04:00:00







>> > Do you know, how I can create Bourne Shell script, that kills every
>> > child-process, that it has started as background processes. Now it is
>> > done like this:

>>    1. You are not running a Bourne shell.  You are running a posix
>>       shell

> I am running ash, to be exact. AFAIK it can do everything, that
> original Bourne Shell can do and everything that POSIX requires and
> not very much in addition.

>>    2. Useless use of awk:

>>       xscreensaver-command -watch | while read foo waste

>>    3. Useless use of test:
>>       do
>>       case $foo in
>>          BLANK) ...;;
>>          LOCK) ...;;
>>          UNBLANK) ...;;
>>          *)
>>       esac

> Thanks. Fixed.

>>    4. You are not running any background processes.
>>    xscreensaver-command will not survive the script.


> % ps xf                                                            6672 | pts/4
>   PID TTY      STAT   TIME COMMAND
> (Clip)
>  3300 ?        S      0:00 ash /home/juhtolv/textures/watch.sh
>  3303 ?        S      0:00  \_ xscreensaver-command -watch
>  3304 ?        S      0:00  \_ ash /home/juhtolv/textures/watch.sh
> (Clip)

> Isn't that PID 3304 background process?

No,  it's in the foreground.  The script will not continue
until xscreensaver-command exits.

Quote:

>  * * *

> Well, what if I really have many child processes as background
> processes?  How can I kill them all, when my script exits? It is
> possible to put PID of that process to environment variable, like
> this:

> $WATCHER &
> WATCHER_PID="$!"

> But what if I have two or more of them? Should I put those PIDs to
> array?  What POSIX says about arrays?

You don't need an array:

   WATCHER_PIDS="${WATCHER_PIDS}${WATCHER_PIDS:+ }$!"

or you can simply "kill -term -$$" upon exiting and that will nail
every process started by the script.

--
Dan Mercer

 
 
 

1. killing a shell script is not killing child processes

dispatcher (NOHUP mode)->daemon->shell script (parent
PID)->executables(child PIDs)

When we try to kill shell script (parent PID) with kill-9 command, I
am expecting that all executables (child PIDs) that are running from
shell script should also be killed.

Shell script process is getting killed but not the child processes.

I tried read all the messages patiently but it added up more
confusions.

I found a way to kill all child processes before killing parent shell
script but trying to find a answer for this behaviour.

Any expert responses are welcome.

Cheers,
Srinivas

2. Looking for Unix MacIP gateway s.w.

3. domain NOT BOUND error??????

4. SEGV not killing child process of shell script

5. Solaris(2.3) Slow

6. how to kill a child process that runs other children processes

7. Linux Fortran Errors

8. How can kill all child processes without killing parent process ?

9. Bourne Shell INTR kills children

10. Killing a background process in Bourne shell

11. Killing a background process in Bourne shell...

12. Forked process being killed by Bourne Shell???