Kill a Python Process

Kill a Python Process

  1. Kill a Python Process Using a Keyboard Shortcut
  2. Kill a Python Process Using the Process Name and the kill Command
  3. Kill a Python Process Using the Process Name and the killall Command
  4. Kill a Python Process Using the Process Name and the pkill Command
  5. Conclusion

While programming in Python, there are situations where our program runs into an infinite loop. In this situation, we need to terminate the program manually.

This article will discuss different ways to kill a Python process.

Kill a Python Process Using a Keyboard Shortcut

The easiest way to kill a Python process is to use the keyboard shortcut CTRL+C.

Whenever a Python program runs into an infinite loop, you can press CTRL+C in the IDE or the terminal in which the program is running. After pressing the keys, the Python process will immediately get terminated.

Sometimes, if a Python program is busy executing system calls, you cannot terminate it normally. We must manually terminate the Python process from the command line in such cases.

Using the command-line statements, we need to send a SIGTERM signal to the program to terminate. Let us discuss different ways to kill a Python process using the command line.

Kill a Python Process Using the Process Name and the kill Command

We will follow these steps to kill a Python process using the kill command in Linux. First, we will list all the running Python processes using the ps command and grep command, as shown below.

Kill Python Process

Here, the ps command first lists all the running processes. The grep command filters all the processes with Python in their name, then shows the output to the user.

You can see that the second term in the output is a number. This number is the process ID of the Python programs.

Using the following syntax, we can use the process_id and the kill command to kill the Python process.

kill process_id

Here, process_id is the process id of the program we want to terminate. You can terminate all the Python processes by using the kill statement and the process IDs of the programs.

For instance, we can kill the third Python process in the above image using the following command:

kill 9146

Kill a Python Process Using the Process Name and the killall Command

Instead of manually terminating the Python process using the kill statement one by one, we can use the killall command to kill all Python processes simultaneously.

The killall command takes the name of a process as input. After execution, it kills all the processes with the given name.

You can kill all the Python processes using the killall command, as shown below.

killall python

Kill a Python Process Using the Process Name and the pkill Command

Instead of the killall command, we can use the pkill command to kill a Python process. The pkill command takes the name of the process as an input argument.

After execution, it sends the SIGTERM signal to all the processes with the name given in the input. As a result, all the processes with the given name are terminated.

Using the following statement, you can pass the name python to the pkill command to kill a Python process.

pkill python

Conclusion

In this article, we have discussed different ways to kill a Python process. To kill a specific Python program, you can use the ps and grep commands with the kill command.

To kill all the Python processes at once, you can use the killall command or the pkill command.

Related Article - Python Process

  • Background Process in Python
  • Perform a Parallel Processing in Python
  • Call External Program in Python