In today’s post I will be showing you how to locate processes as-well as how to terminate them using the Linux command line. So lets jump right in by learning how to locate a process. For the purpose of this guide I will mostly be using Firefox as the example process.
Finding Processes In Linux
There are a ton of ways to locate processes in Linux, one of them being to use the ps command. If we run ps aux then we will be presented with a list of all processes, as-well as information such as PID (Process ID), State (For example Z for zombies or D for waiting on IO), and more. Since we already know we are looking for Firefox we can pipe the output into grep which will show any line that contains the phrase firefox, I also added MEM so we would have a nice header. Here is an example below.
ps aux | grep -E "[M]EM|[f]irefox"
This is great if you know what process you are looking for but what if you don’t? For example if your server is showing almost no free memory, how can we find the process responsible for gobbling up all of your RAM?
You can actually sort the output of the PS command by CPU usage, Memory Usage, etc. Since we only care about what is eating up all of our RAM in this example, we will pipe the output into head which will limit our number of results, we simply don’t need to know about the rest of the processes.
ps aux --sort -rss | head
As you can see we found that Firefox is using up the most RAM out of all other processes. But now that we found the problem how do we get rid of it? We kill it.
Killing Processes In Linux
Killing a process is basically the same thing as pressing “End Task” in Windows Task Manager. Here are 2 of the commands that will kill a process or multiple processes. Here are a few of them and how to use them. For this example Firefox has the PID 2453. I recommend adding a success message using && simply because you may not get a confirmation that the process was terminated otherwise.
kill 2453 && echo Killed
killall firefox && echo Killed
By default this will try to kill a process safely (same as adding argument -15), if this fails then add a -9 like the following example.
kill -9 2453 && echo Killed
killall -9 firefox && echo Killed
The way that you would identify a zombie is by it’s value in the STAT column, zombies will have a Z. One thing to note is that you cannot kill zombies, there is only one way that I can think of to get rid of them other than a reboot.
While you can’t kill a zombie directly, you can kill its parent process which will make init it’s new parent and then init will reap/kill the zombie. To find a processes parent ID or PID you would use the following command once you know the PID of the actual zombie.
ps -o ppid= -p 2453
I hope this post was useful and please don’t forget to comment or like/share this post. Thanks!