zsh-workers
 help / color / mirror / code / Atom feed
* Problems with background jobs in a script.
@ 2008-08-01 12:18 Dave Yost
  2008-08-02 22:55 ` Bart Schaefer
  2008-08-03 11:27 ` Peter Stephenson
  0 siblings, 2 replies; 3+ messages in thread
From: Dave Yost @ 2008-08-01 12:18 UTC (permalink / raw)
  To: zsh-workers


The script below exhibits several problems.

All I'm trying to do is to get a script to recursively kill all of 
its background processes on exit. (In my case, this should be an 
issue only when the script is killed.)

My zsh version is 4.3.4

Thanks

Dave

  - - - - - - -

0 402 Z% /tmp/,x
[1]  - 38831 running    sleep 1000
[2]  + 38832 running    sleep 2000
/tmp/,x:kill:14: %3: no such job
The killed jobs still show up after the kills.
If we wait a bit, the jobs don't show up.
Now we start a sleep in the background with ()
Now we've killed it, but it's still running.
38837 s000  RN+    0:00.00 sleep 10
38839 s000  R+     0:00.00 grep sleep
0 403 Z%

  - - - - - - -

#!/bin/zsh

# Problems with background jobs in a script.

sleep 1000 &
sleep 2000 &

# This prints OK
jobs -l

# This prints nothing.
jobs -l | awk '{ print $3  $0 }'

kill %3
kill %2
kill %1

echo The killed jobs still show up after the kills.
jobs -l

echo "If we wait a bit, the jobs don't show up."
sleep 1
jobs -l

echo Now we start a sleep in the background with '()'

(
   sleep 10
   TRAPINT() {
     echo killing the sleep
     kill %%
   }
) &

kill %%
echo "Now we've killed it, but it's still running."
ps x | grep sleep

exit

# What I want to do is something like this:

TRAPEXIT() {
   kill $(
     jobs -l | awk '{ print $3 }'
   )
}

# or

TRAPEXIT() {
   while kill %% ; do ; done
}


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Problems with background jobs in a script.
  2008-08-01 12:18 Problems with background jobs in a script Dave Yost
@ 2008-08-02 22:55 ` Bart Schaefer
  2008-08-03 11:27 ` Peter Stephenson
  1 sibling, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2008-08-02 22:55 UTC (permalink / raw)
  To: zsh-workers

On Aug 1,  5:18am, Dave Yost wrote:
} 
} All I'm trying to do is to get a script to recursively kill all of 
} its background processes on exit. (In my case, this should be an 
} issue only when the script is killed.)

Is there a reason that killing the process group and letting the OS
deal with propagating the signals will not work?

    TRAPEXIT() { kill -1 0 }
 
(Change -1 to whatever signal you wanted to send.)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Problems with background jobs in a script.
  2008-08-01 12:18 Problems with background jobs in a script Dave Yost
  2008-08-02 22:55 ` Bart Schaefer
@ 2008-08-03 11:27 ` Peter Stephenson
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2008-08-03 11:27 UTC (permalink / raw)
  To: zsh-workers

On Fri, 1 Aug 2008 05:18:16 -0700
Dave Yost <Dave@Yost.com> wrote:
> The script below exhibits several problems.
> 
> All I'm trying to do is to get a script to recursively kill all of 
> its background processes on exit. (In my case, this should be an 
> issue only when the script is killed.)

I'm actually suprised it gets as far as it does.  Essentially no thought
has been put into to job control for non-interactive shells (when the
MONITOR option is not set); it so happens zsh still uses its job table
for storing information about jobs it's started, but many of the fixes
to make things appear seamless (or at least more closely stitched) only
work with MONITOR.  That may be fixable to some extent (particularly
with background jobs where the task is already separate from the main
shell even without the normal job control OS support) but there's so
much to do to the shell I shouldn't hold your breath.

You could try pretending the script is interactive (with the -i option
--- I tried this it and certainly made a better stab at your script) but
that may have side effects.
 
Possibly it's simpler to keep track of the process numbers you need to
kill by recording them from $!.  However, since in this case the shell
doesn't guarantee to tell you when the process is terminated you can't
be absolutely sure you're killing the right process.

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-08-03 11:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-01 12:18 Problems with background jobs in a script Dave Yost
2008-08-02 22:55 ` Bart Schaefer
2008-08-03 11:27 ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).