Re: "kill" group of commands from within script

From: Chris F.A. Johnson (cfajohnson_at_gmail.com)
Date: 09/01/05


Date: Thu, 1 Sep 2005 14:11:30 -0400

On 2005-09-01, Heiner Steven wrote:
> 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?

    In bash, if you use the dynamically loadable builtin for sleep, it will
    work.

-- 
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>


Relevant Pages

  • Re: Newbie help for using multiprocessing and subprocess packages for creating child processes
    ... the first is the conceptual equivalent of executing the ... The first searches for a command in your PATH named ‘ls /path...’. ... where STANAME is a string 4 characters long. ... firing off my shell script: ...
    (comp.lang.python)
  • Re: Newbie help for using multiprocessing and subprocess packages for creating child processes
    ... The first searches for a command in your PATH named ‘ls /path...’. ... where STANAME is a string 4 characters long. ... firing off my shell script: ...
    (comp.lang.python)
  • print subsystem errors
    ... I have an application that executes a shell script to print jobs. ... I can take the same command issued in the shell script; ... ATTENTION ATTENTION ATTENTION ATTENTION ATTENTION ...
    (AIX-L)
  • Re: despair
    ... I once tried to write a cd program in Unix. ... Just putting a cd command in a script and then ... hell are you supposed to write shell script macros to run a frequently ... make "$ FOO BAR" a meaningful DCL command. ...
    (comp.os.vms)
  • Re: cron and command quote
    ... if you are going to be using a shell script you have to make sure that it exits properly. ... So, '||' is just as legal as '&&' and would do just as it does on the command line, assuming of course there is nothing found by the grep. ... with && we have execute command1 if it returns true execute command2 ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx with a subject of "unsubscribe". ...
    (Debian-User)