You might need to figure out how to kill all python processes on Linux if the system malfunctions or causes errors. And we’re here to help you fix just that.
Quite often, when you are running programs on your system, multiple python processes can spawn through different processes or users. And it becomes quite difficult to find the instances, for example, if you want to stop them.
Python: The Basics
The Linux operating system provides various commands (such as pkill
, kill
, killall
) that can stop or kill a process such as python, (sometimes without even knowing the process ID of an individual process). The command works by locating the process based on the specific criteria and kills them.
There are also commands (such as top
, ps
, pgrep
) that output the processes based on your specific criteria. Furthermore, if the currently running process is causing the problem, you can always kill it by pressing “Ctrl + Z
”.
Why should you kill a process?
There are various reasons for killing a specific process in an operating system. The process might be taking precious resources such as memory and CPU cycles, or the process may have opened or locked shared files. Additionally, the process might malfunction or behave abnormally. So you can stop and restart the process again.
Finally, as a developer or testing engineer, you may be using various libraries and programs. And you may want to check the resource consumption of various programs by starting and stopping them frequently.
How to Kill all Python Processes on Linux
Killing all the python processes using pkill
The pkill
command was developed initially for Solaris but is now available on Linux. Today, it is preinstalled on Linux systems and is available as part of the procps-ng
or props package. The command sends signals to the running system considering the parameters provided to the command.
You can provide the complete or partial name of the process. What you have to do first is open the Terminal in Linux. You can do this by right-clicking on the desktop and choosing Terminal. In the Terminal, type the following command:
$ sudo pkill python
This command will kill all the python processes running on your system irrespective of the owner of the process. Since you are running this command as a super user, you will be prompted for the password.
You can also use the -e
option to see the details of the processes being killed. The following command illustrates that:
$sudo pkill -e python
Killing the python processes using killall
killall
is another Linux command similar to pkill
. You just need to provide the name of the process or attributes of the process you want to stop. To stop the python processes, you can use the following commands on the Terminal:
$ sudo killall python
Ensure all python processes have been stopped
Now, we will discuss how one can ensure all the python processes have stopped. Use the following command to list down all the python processes:
$sudo grep python
This should produce no output as in the previous step you have stopped all the python processes.
Killing a specific process
You may also want to find the process ID of the specific python process causing problems and then kill that process. Type the following command to see the list of processes:
$ ps -ef
To filter out only python processes, you can also use the following command:
$ ps -ef|grep python
The pipe command is used to separate two Linux commands, and the output of the left command is passed as input to the right. So first, the list of the processes is extracted, and then filtered based on the keyword python. You should see an output similar to the following:
Note down the ID of the process shown in the second column. Now, you can use the kill command (located in /bin/kill
) to stop the process by providing the id as an argument, such as:
$ kill -9 2431
The argument 9 passed to the kill command is used to force kill a process. The kill command, by default, sends the TERM
signal to the process. Besides, there are other signals as well, such as HUP
and KILL
.
Tip: You can also use the top command to get the list of all processes.
Killing a process based on the name of the script
You can kill a process based on the name of the python script. You can use the following set of commands to delete a process based on the script’s name:
ps -ef|grep "script_name"|awk'{print$2}'|xargs sudo kill
To kill all the python scripts, you can use the following commands:
ps -ef|grep "python"|awk'{print$2}'|xargs sudo kill
This article discusses how you can stop all the python processes on a Linux system. Stopping processes is crucial in any operating system as it can consume precious resources. We also discussed pkill and killall commands available in Linux to stop the processes.
We even discussed how you could get the process ID of the python process using various commands such as top, ps, and pidof. And how specific processes can be killed using the kill command.
If this guide helped you, please share it.