Skip to content Skip to sidebar Skip to footer

Kill Process And Its Sub/co-processes By Getting Their Parent Pid By Python Script

i am using MULTIPROCESSING to find my requirement. And when that runs i am getting a pid(may be parent! i don't know what to call that) then co processes with their own pid and ref

Solution 1:

From your question, I am not able to understand; however I am assuming that you have process id handy e.g. in your example, you have process id 2222. Then you can try this:

#Assuming you have parent_pid i.e. from your example you have 2222
pname = psutil.Process(parent_pid)
cpid = pname.get_children(recursive=True)
for pid in cpid:
    os.kill(pid.pid, signal_num) #signal_num is the signal you want to send i.e. 9 as per your example

Please take care of exception handling e.g. check if process/child-process exist before you kill it etc using try - except block.

Post a Comment for "Kill Process And Its Sub/co-processes By Getting Their Parent Pid By Python Script"