zsh-workers
 help / color / mirror / code / Atom feed
From: Dave Yost <Dave@Yost.com>
To: zsh-workers@sunsite.dk
Subject: Problems with background jobs in a script.
Date: Fri, 1 Aug 2008 05:18:16 -0700	[thread overview]
Message-ID: <pdy98830624086ac4b89c662c83@[192.168.1.2]> (raw)


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
}


             reply	other threads:[~2008-08-01 12:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-01 12:18 Dave Yost [this message]
2008-08-02 22:55 ` Bart Schaefer
2008-08-03 11:27 ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='pdy98830624086ac4b89c662c83@[192.168.1.2]' \
    --to=dave@yost.com \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).