In Linux, if you’d like to kill a command and all processes spawned by that command do this (in our example the command “parent” is example.py):
1 2 3 4 5 |
kill -- -$(ps -ae o pid,pgrp,cmd | grep "[e]xample.py" | awk '{print $2}' | tail -1) |
This will kill the “example.py” process and anything that it spawned.
How it works:
To kill all processes of a group the command is
1 2 3 |
kill -- -groupid |
To get the group id you do the ps command using the arguments as shown, grep it for your command, but formatting example.py with quotes and using the bracket for the first letter (this filters out the grep command itself) then filter it through awk to get the second field which is the group id. The tail -1 gets rid of duplicate group ids. Yout put all of that in a variable using the $() syntax and voila – you get the group id. So you substitute that $(mess) for that -groupid in the kill command above.