How to kill all processes opened by shell script with ctrl + C?

I have several python scripts that I start collectively from a shell script as follows:

#!/bin/bash python prog1.py & python prog2.py & python prog3.py 

As I develop, I often want to stop these processes. I usually do this by pressing ctrl + C, but unfortunately several python programs keep (zeromq) sockets open. This means that I need to manually find them (I use lsof -i) and kill them with the PID.

So, I am looking for an easier way to automatically kill these python processes from the shell when I press ctrl + C. In https://stackoverflow.com/a/212928/ ... I found code that supposedly should do what I need. I just don’t understand anything about the code and how I could adapt it to my needs.

Will anyone be so kind as to help me here?

 cat >work.py <<'EOF' import sys, time, signal signal.signal(signal.SIGINT, signal.SIG_DFL) for i in range(10): time.sleep(1) print "Tick from", sys.argv[1] EOF chmod +x work.py function process { python ./work.py $1 } process one & wait $! echo "All done!" 
+7
source share
1 answer

Let the bash script catch SIGINT and kill everything in the current process group:

 intexit() { # Kill all subprocesses (all processes in the current process group) kill -HUP -$$ } hupexit() { # HUP'd (probably by intexit) echo echo "Interrupted" exit } trap hupexit HUP trap intexit INT python prog1.py & python prog2.py & python prog3.py & wait 
+11
source

All Articles