Search code examples
bashcygwin

How do I terminate all the subshell processes?


I have a bash script to test how a server performs under load.

num=1
if [ $# -gt 0 ]; then
    num=$1
fi
for i in {1 .. $num}; do
    (while true; do
        { time curl --silent 'http://localhost'; } 2>&1 | grep real
    done) &
done        

wait

When I hit Ctrl-C, the main process exits, but the background loops keep running. How do I make them all exit? Or is there a better way of spawning a configurable number of logic loops executing in parallel?


Solution

  • Here's a simpler solution -- just add the following line at the top of your script:

    trap "kill 0" SIGINT
    

    Killing 0 sends the signal to all processes in the current process group.