This quick tutorial teaches you to kill a process in Linux using its process id. This is particularly helpful in killing unresponsive programs.
You can easily stop a program in Linux terminal by pressing the Ctrl+C keys. But it often happens that you need to ‘kill’ an unresponsive program. In Windows you have the task manager for this situation. You can use task manager in Linux as well but the command line way is a lot better and effective in handling unresponsive programs.
Using the terminal is better because GUI based tools may not show the still running hidden process. GUI tools may not be available if you are using the server edition of a Linux system.
How to kill a process in Linux
If you run an application, it runs some process in the background. If you want to close this application forcefully, you can kill the process associated to it.
To kill a process, you need to know the its process ID (PID). The next section tells you how to find the process ID of a program.
Step 1: Find the process ID (PID) of the program
There are several ways you can use for finding the PID of a process.
If you know the name of the process, you can use the command pidof in this fashion:
You can take help of the tab completion to find the name of the program. The good thing about this command is that it will give the PID of all the processes initiated by the program. Here’s an example:
9734 9718 9716 9708 9622 9619
If the pidof command doesn’t result anything, it could mean either there is no process running of that program or the program name you used is incorrect.
If you are unaware of the exact program name, you can try the ps command. This ps command is used for seeing the running processes on the system. You can use the grep command with the program name (or whatever you remember about it).
ps aux | grep -i “name of your desired program”
ps aux command returns all the running process on the system. And the grep afterwards shows the line which matches with the program name. The output of the command will be like this:
As shown in the picture above, you can get the process ID of the program/process in the second column. Just ignore the line with “–color =auto”.
Step 2: Kill the process using the PID
Once you have the PID of the desired application, use the following command to kill the process:
sudo kill -9 process_id
If you have more than one process id, you can kill all of them together by providing all the PIDs.
sudo kill -9 process_id_1 process_id_2 process_id_3
You can also combine the kill command the pidof command to kill all the process of a program.
sudo kill -9 `pidof programe_name`
Of course, you have to replace the program_name with the name of the program you want to kill.
Bonus Tip: Use killall to kill all the process of a given program
If you know the name of the program, you can use the magnificent killall command and kill all the processes of that program in one single command.
How do you kill programs in Linux?
I hope this quick little tutorial helped you. What is your favorite way to kill a program in Linux? Task manager, kill, killall or xkill?