How to Kill a Python Process
- Kill a Python Process Using a Keyboard Shortcut
-
Kill a Python Process Using the Process Name and the
kill
Command -
Kill a Python Process Using the Process Name and the
killall
Command -
Kill a Python Process Using the Process Name and the
pkill
Command - 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.
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.
Aditya Raj is a highly skilled technical professional with a background in IT and business, holding an Integrated B.Tech (IT) and MBA (IT) from the Indian Institute of Information Technology Allahabad. With a solid foundation in data analytics, programming languages (C, Java, Python), and software environments, Aditya has excelled in various roles. He has significant experience as a Technical Content Writer for Python on multiple platforms and has interned in data analytics at Apollo Clinics. His projects demonstrate a keen interest in cutting-edge technology and problem-solving, showcasing his proficiency in areas like data mining and software development. Aditya's achievements include securing a top position in a project demonstration competition and gaining certifications in Python, SQL, and digital marketing fundamentals.
GitHub