Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Problems with background jobs in a script.




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
}



Messages sorted by: Reverse Date, Date, Thread, Author