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?
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.
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.
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.
The pkill
utility can look at command lines when sending signals:
pkill -f xxx.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With