Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill a running python process? [duplicate]

How to kill a running python in shell script when we know the python file name xxx.py? (it is executed by cmd python xxx.py)

I can use ps aux | grep python to get the pid of it. and then kill pid to terminate it.

but every time, I have to execute two cmd. Is there a good way to do it?

like image 866
HenrySnoopy Avatar asked Oct 23 '13 12:10

HenrySnoopy


People also ask

How do you kill a Python process that is running?

Alternatively, you can use the top command to find the python process. Simply enter k (for kill) and the top program will prompt you for the PID of the process to kill.

How do I stop a Python code from running?

To stop code execution in Python you first need to import the sys object. After this you can then call the exit() method to stop the program from running. It is the most reliable, cross-platform way of stopping code execution.

How do I get rid of subprocess?

The killpg() method send the signal to all the process group to terminate the process. Among all these commands, killpg is the method used to kill all the subprocesses. We can also use for loop to kill all the processes by using the kill() method or terminate() method.


1 Answers

The pkill utility can look at command lines when sending signals:

pkill -f xxx.py 
like image 142
AKX Avatar answered Sep 28 '22 00:09

AKX