Every application and service on your Linux machine runs as a process. And, most of the time, these processes work smoothly. However, sometimes a process (an application or service) crashes and slows down your system or causes other problems (like a desktop lock).
When this happens, you need to be able to kill the processes that run amok. Most Linux desktop environments today include a tool with a GUI that allows you to end a process by selecting it and choosing Kill.
Image: Jack Wallen
The GUI is all well and good, but what happens when you can’t access it due to a memory leak that overloads your computer? In these cases, we can turn to the command line.
There are two easy ways to kill a Linux process from the command line.
How to use the kill command
The first method uses the kill command. The kill command kills processes by their PID (process ID). A typical kill command looks like this:
kill pid
Where PID is the process ID in question.
There, you are probably wondering: “where can I find the PID of my application?” Good question. Assume 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:
-
ps: gives a record of running processes. -
to : lift the BSD “only yourself” restriction as well as the BSD “must have a tty” restriction and list all processes in the user list. -
| : binds the output of the ps command to the next command (in this case, grep) -
grep: matches the process with the name that follows. -
firefox: the process we are looking for.
In the case of Firefox, you will see a process for each tab you have open. To kill Firefox itself, you need to locate the PID of the very first process listed. This list will look something like this:
jack 21960 7.6 2.5 14450944 825944? SNL Jun12 122:44 firefox
The PID is the first digit (directly to the right of the username). So for the example above, the kill command would be:
kill 21960
The above command should kill Firefox.
How to use the killall command
This method is considerably simpler. Instead of using the process PID, you use the process name. So, if we want to kill the Firefox process, the command will be:
killall firefox
If you want to be sure, you can force killall to ask for validation before killing a process using the interactive option like this:
killall -i firefox
Answer yes to the question and the Firefox process will be killed.
That’s it, it’s that easy to kill a process in Linux. There are other options available for each of these commands, but you should be fine, at least initially, with this. To learn more about each command, read the man pages with man kill and man killall.
Source: “ZDNet.com”