"kill" group of commands from within script

From: Heiner Steven (heiner.steven_at_odn.de)
Date: 09/01/05


Date: Thu, 01 Sep 2005 16:08:19 +0200

I have a problem with a small shell script. It starts two
commands in the background, and then tries to terminate them
both using the kill(1) command:

==========(timeoutprob)==========
:
# Start process in the background
(sleep 10; echo WAKEUP) & bgpid=$!

sleep 1

# Terminate it
kill $bgpid
=================================

When I run this command using KornShell, the script works
as expected: the "kill" command in line 8 terminates the
background processes, i.e. both "sleep" and "echo".

Using BASH or Z Shell the "sleep" command gets inherited
by the "init" process (process id "1"), and continues running.
This results in many "sleep" processes running at a given time.

How can I write the script in a way that BASH terminates
the "sleep" command, too?

Background of the problem:

     o (sleep 10; echo WAKEUP) &
        creates more than one process:
         (a) background process owning "sleep" and echo
         (b) a process for "sleep"
            (maybe another process for "echo").

         Example:
           pid process
           1000 background process
           1001 sleep
           1002 echo

      o the process id "bgpid" is always the one of the background
         process (here: 1000)

When I now send SIGTERM to process 1000, ("kill 1000"), the
behaviour is different for KornShell on the one, and BASH/Z Shell
on the other hand.

  o KornShell seems to send the signal to the process group,
     effectively terminating all processes
  o BASH/Z Shell send the signal to the parent of the group (here: 1000).
     The children processes survive, and, having lost their parent,
     are inherited by "init"

Does anybody have an idea how I could rewrite the script (without
touching BASH/Z Shell source code) for terminating the "sleep", too?

Heiner

-- 
  ___ _
/ __| |_ _____ _____ _ _     Heiner STEVEN <heiner.steven@nexgo.de>
\__ \  _/ -_) V / -_) ' \    Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_|   http://www.shelldorado.com/


Relevant Pages

  • Re: Newbie getting desperate with for
    ... This tells the shell to run the script ... When you run a command from the shell, it first tries to directly execv ... which calls the kernel to load and execute the file. ...
    (comp.lang.python)
  • Re: shell scripting
    ... and am assuming an sh-compatible shell here.... ... command 1 options arguments ... To run the script, do: ... then you need to use the full path to execute it: ...
    (comp.unix.questions)
  • Re: shell scripting
    ... and am assuming an sh-compatible shell here.... ... command 1 options arguments ... To run the script, do: ... then you need to use the full path to execute it: ...
    (comp.unix.shell)
  • Re: [opensuse] Lazarus and free pascal need ELF
    ... In a bourne shell interpreter, which is what you are in while you are logged in and sitting at a command prompt, the syntax "dot space filename" does not execute the file, it sources the file. ... It has the effect that your current shell instance ends up directly executing the commands, which may look and smell like the normal way a script or program is executed, but it's in fact quite different. ... The simplest example of why this is "wrong" even though it seems to "work for scripts" would be to look at any typical script that has an exit command. ...
    (SuSE)
  • Re: [opensuse] Lazarus and free pascal need ELF
    ... Lazarus the following in command line.. ... In a bourne shell interpreter, which is what you are in while you are ... Sourcing a file is usually meant for a script to read in some variables ... exit command. ...
    (SuSE)