Every application and service on your Linux machine runs as a process. And, for the most part, those processes work pretty well. From time to time, however, you may encounter a process (an application or a service) that goes astray and slows down your system or causes other problems (such as a locked desktop).
When that happens, you need to be able to kill those runaway processes. Now, most Linux desktop environments include a GUI tool that makes killing a process a simple matter of selecting the process and then selecting Kill (Figure 1).
That’s all well and good, but what happens when you can’t access the GUI because a runaway process is eating up your system’s memory? That’s when you go to the command line.
I’ll show you two easy ways to kill a Linux process from the command line. You will be surprised how easy it really is.
How to use the kill command
The first method I’m going to show you uses the kill command. The kill command kills processes via their PID (Process ID). A typical delete command looks like this:
Where PID is the process ID for the process in question.
You are probably wondering, “Where do I locate the PID?” Good question. That is how. Let’s say the problematic application is the Firefox web browser. To kill Firefox with the kill command, open a terminal window and locate the PID with:
ps aux |grep firefox
The breakdown of the above command is simple:
- P.S: reports a snapshot of the currently running processes.
- assistant: removes the BSD-style “only yourself” restriction, as well as the BSD-style “must have a tty” restriction, and lists all processes in the user list.
- |: pipe the output of ps to the following command (in this case, grep)
- grep: match only the process with the sting that follows.
- firefox: the process we are looking for.
Of course, in the case of Firefox, you’ll see a process for each tab you have open. To really kill Firefox, you need to locate the PID of the first one in the list. That list will look like this:
jack 21960 7.6 2.5 14450944 825944 ? SNl Jun12 122:44 firefox
The PID is the first number (directly to the right of the username). So for the example above, the delete command would be:
The above command should kill Firefox.
How to use the killall command
This method is considerably easier. Instead of using the PID of the process, use the name of the process. So if we want to kill the process called firefox, the command would be:
If you want to be sure, you can force killall to check that you want to kill the command using the interactive option like so:
Answer the question now and the Firefox process will exit.
Believe it or not, it’s that easy to kill a runaway process (or any other process) in Linux. Yes, there are more options available for each of those commands, but what I’ve outlined above will get you started. For more information about each command, read the man pages with man kill and man killall.