Kill python interpeter in linux from the terminal

PythonLinuxTerminalCommand

Python Problem Overview


I want to kill python interpeter - The intention is that all the python files that are running in this moment will stop (without any informantion about this files). obviously the processes should be closed.

Any idea as delete files in python or destroy the interpeter is ok :D (I am working with virtual machine). I need it from the terminal because i write c code and i use linux commands... Hope for help

Python Solutions


Solution 1 - Python

pkill -9 python

should kill any running python process.

Solution 2 - Python

There's a rather crude way of doing this, but be careful because first, this relies on python interpreter process identifying themselves as python, and second, it has the concomitant effect of also killing any other processes identified by that name.

In short, you can kill all python interpreters by typing this into your shell (make sure you read the caveats above!):

ps aux | grep python | grep -v "grep python" | awk '{print $2}' | xargs kill -9

To break this down, this is how it works. The first bit, ps aux | grep python | grep -v "grep python", gets the list of all processes calling themselves python, with the grep -v making sure that the grep command you just ran isn't also included in the output. Next, we use awk to get the second column of the output, which has the process ID's. Finally, these processes are all (rather unceremoniously) killed by supplying each of them with kill -9.

Solution 3 - Python

pkill with script path

pkill -9 -f path/to/my_script.py

is a short and selective method that is more likely to only kill the interpreter running a given script.

See also: https://unix.stackexchange.com/questions/31107/linux-kill-process-based-on-arguments

Solution 4 - Python

You can try the killall command:

killall python

Solution 5 - Python

pgrep -f <your process name> | xargs kill -9

This will kill the your process service. In my case it is

pgrep -f python | xargs kill -9

Solution 6 - Python

pgrep -f youAppFile.py | xargs kill -9

pgrep returns the PID of the specific file will only kill the specific application.

Solution 7 - Python

If you want to show the name of processes and kill them by the command of the kill, I recommended using this script to kill all python3 running process and set your ram memory free :

ps auxww | grep 'python3' | awk '{print $2}' | xargs kill -9

Solution 8 - Python

to kill python script while using ubuntu 20.04.2 intead of Ctrl + C just push together

Ctrl + D

Solution 9 - Python

I have seen the pkill command as the top answer. While that is all great, I still try to tread carefully (since, I might be risking my machine whilst killing processes) and follow the below approach:

First list all the python processes using:

$ ps -ef | grep python

Just to have a look at what root user processes were running beforehand and to cross-check later, if they are still running (after I'm done! :D)

then using pgrep as :

$ pgrep -u <username> python -d ' ' #this gets me all the python processes running for user username

# eg output:
11265 11457 11722 11723 11724 11725

And finally, I kill these processes by using the kill command after cross-checking with the output of ps -ef| ...

kill -9 PID1 PID2 PID3 ... 

# example 
kill -9 11265 11457 11722 11723 11724 11725

> Also, we can cross check the root PIDs by using :

pgrep -u root python -d ' '

and verifying with the output from ps -ef| ...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionאריאל ליטמנוביץView Question on Stackoverflow
Solution 1 - PythonLorenzo BaracchiView Answer on Stackoverflow
Solution 2 - PythonDhruv KapoorView Answer on Stackoverflow
Solution 3 - PythonCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 4 - PythonpkacprzakView Answer on Stackoverflow
Solution 5 - PythonMahesh NarwadeView Answer on Stackoverflow
Solution 6 - PythonRoberto MedranoView Answer on Stackoverflow
Solution 7 - PythonTaha HamedaniView Answer on Stackoverflow
Solution 8 - PythonmarcdahanView Answer on Stackoverflow
Solution 9 - Pythonaspiring1View Answer on Stackoverflow