It is inevitable that at some point you'll need to find and kill processes running on Linux. Like any other operating system, the programs or applications run on it can lock up, malfunction and become unresponsive. At which point you'll want to locate the process and stop or terminate it. Other use case examples might be to free up system resources for other tasks or to put a halt to security threats.
I personally know of people who go straight for the power button when they encounter a locked up application. In my mind, this is not good. We should always try to isolate and terminate a malfunctioning application before proceeding to soft reboot. And hard resets should be reserved as a last resort.
Good reasons to Kill Processes running on Linux
There are several good reasons to terminate, stop, or pause running processes. Here are just a few of them.
To free up system resources
When a process is running, it consumes system resources such as CPU time and memory. If too many processes are running at the same time, it can slow down the system. Killing unnecessary ones can free up system resources and improve performance.
To stop a malfunctioning program
As previously mentioned, sometimes, a program or application may become unresponsive or start consuming too many resources, causing the system to slow down. Killing the process can stop the program and prevent it from causing further problems.
To terminate a security threat
Malware or unauthorized processes may be running on the system and compromise its security. Killing those can stop the malware or unauthorized process from running and prevent further damage to the system.
To perform system maintenance
Some processes may need to be stopped temporarily during system maintenance, such as updates or upgrades. Killing or pausing these can allow the maintenance tasks to be performed smoothly.
In summary, finding and killing Linux processes can be useful for improving system performance, maintaining security, troubleshooting problems, and performing system maintenance tasks.
How to Find Processes running on Linux
To find all processes that are running on a system, you can use the ps or top commands:
ps command
The "ps command" can be used to show a list of all running processes on the system along with their process ID (PID) numbers. To display them in a list using ps:
First open a terminal Ctrl+Alt+T (if you're running from Ubuntu or Debian). Then type ps aux and press Enter.
ps aux
This ps aux command will display a list of all processes running on the system. You can use the PID number to kill a process.
top command
The "top command" displays real-time system information, including a list of running processes.
To display the list of processes using top, type:
top
This top command will display a real time list of processes, along with their PIDs, CPU usage, memory usage, and other information.
How to Kill a Process in Linux
You use the kill PID command to kill processes that are running in Linux.
kill command
The "kill command" is used to terminate a process. To kill a process, you need to know its PID number. You can use either the ps command or top command mentioned above to find the PID number of the process you want to terminate. Once you have the PID number, type kill followed by the PID number.kill PID
Replacing kill PID with the actual PID number of the process you want to terminate.
For example, to terminate a process with PID number 1090, you would type the following command:
kill 1090
If you want to force a process to terminate, you can use "kill -9" instead:
kill -9
Note that the kill -9 command is a forceful way of terminating a process, as it sends a signal to the process to immediately terminate without allowing it to clean up any resources it may have allocated. This can sometimes lead to data loss or other issues, so it should be used with much caution.