Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to kill process and child processes from python?

Tags:

python

for example from bash:

 kill -9 -PID  

os.kill(pid, signal.SIGKILL) kill only parent process.

like image 213
Bdfy Avatar asked Jul 01 '11 15:07

Bdfy


People also ask

How do you kill a process in Python?

A process can be killed by calling the Process. kill() function. The call will only terminate the target process, not child processes.

How do you kill a process using the child process?

For killing a child process after a given timeout, we can use the timeout command. It runs the command passed to it and kills it with the SIGTERM signal after the given timeout. In case we want to send a different signal like SIGINT to the process, we can use the –signal flag.

How do you kill a process in Python multiprocessing?

We can kill or terminate a process immediately by using the terminate() method. We will use this method to terminate the child process, which has been created with the help of function, immediately before completing its execution.

How do you stop a Python process from running?

You can use ps aux | grep python to determine the running Python processes and then use kill <pid> command for sending a SIGTERM signal to the system. To kill the program by file name: pkill -f python-script-name.

How do I Kill a process tree in Python?

you should use signal parameter 9 to kill the process tree. root@localhost:~$ python >>> import os >>> os.kill (pid, 9) if you should use signal.SIGKILL constant, you should use os.killpg (pgid, signal.SIGKILL) to kill the process tree.

How to kill all processes in a process group?

If you want to kill all processes which includes in process group then you should use parent process id. Like that:

How to kill a process with a negative PID?

When you pass a negative PID to kill, it actually sends the signal to the process group by that (absolute) number. You do the equivalent with os.killpg () in Python. Show activity on this post. Another solution if your process is not a process group and you don't want to use psutil, is to run this shell command: Show activity on this post.

How do I Kill a process on Linux?

I use SIGKILL on Linux, to kill process immediatly, and SIGTERM on Windows, because there is no SIGKILL on it. Also I used killpg () to kill the whole group of processes on Linux. P.S. Check on Linux, but still doesn't check on Windows, so maybe we need one more additional command for Windows (for example CTRL_C_EVENT or use another answer .)


1 Answers

If the parent process is not a "process group" but you want to kill it with the children, you can use psutil (https://psutil.readthedocs.io/en/latest/#processes). os.killpg cannot identify pid of a non-process-group.

import psutil  parent_pid = 30437   # my example parent = psutil.Process(parent_pid) for child in parent.children(recursive=True):  # or parent.children() for recursive=False     child.kill() parent.kill() 
like image 80
jung rhew Avatar answered Sep 28 '22 00:09

jung rhew