How to kill bash script children when crtl+c
27 Sep 2016Using a script to run multiple tasks in background can be very handy but it can also be anoying when we press crtl+c to interrupt and the background processes keep working. There are many approaches to solve this but I have found the cleanest one is to simply trap the signal:
trap 'kill `jobs -p`; exit' INT
This will first kill all the background processes and then exit the main script (quitting loops as well).