How can I kill and wait for background processes to finish in a shell script when I Ctrl+C it?
- by slipheed
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