How can I kill and wait for background processes to finish in a shell script when I Ctrl+C it?
Posted
by
slipheed
on Server Fault
See other posts from Server Fault
or by slipheed
Published on 2012-11-13T02:30:46Z
Indexed on
2012/11/13
5:03 UTC
Read the original article
Hit count: 592
I'm trying to set up a shell script so that it runs background processes, and when I ctrl+C the shell script, it kills the children, then exits.
The best that I've managed to come up with is this. It appears that kill 0 -INT
also kills the script before the wait happens, so the shell script dies before the children complete.
Any ideas on how I can make this shell script wait for the children to die after sending INT?
#!/bin/bash
trap 'killall' INT
killall() {
echo **** Shutting down... ****
kill 0 -INT
wait # Why doesn't this wait??
echo DONE
}
process1 &
process2 &
process3 &
cat # wait forever
© Server Fault or respective owner