In Bash, I start a number of processes run in the background and have Bash wait in the foreground:
sleep 7 & sleep 10 & wait
(This is actually a simplification of what npm script
does, which is the application of this question.)
Then, whenever I Ctrl+C
out of the wait
process, I would like all background processes killed.
Is this possible at all? How?
The background processes are child processes of the shell, but not child processes of wait
, so there is no good way to propagate a signal to wait
to any of the other processes.
A quick and dirty way to kill all background processes, which you can put into a script or an alias, is
kill -INT $(jobs -l | awk '{print $2}')
kill -INT $(jobs -p)