You can easily stop a program in Linux terminal by pressing the Ctrl+C keys. But you often need to ‘kill’ an unresponsive program.
In Windows, you have the task manager for this situation. In Linux, you can kill a process using the command line or GUI task managers.
Using the command line is easier. All you have to do is:
sudo kill -9 process_ID
The main task here would be to find the process ID (PID). You get that with:
pidof process_name
You can use the ps command or top command if you do not know the exact process name. Using the top command, you can kill the process from there.
Don't worry. In this tutorial, I'll show the following in detail:
- Finding the PID and killing a running process in the command line
- Using the top command to find and kill a process
- Using task managers in Linux desktop to terminate a running process
Let's start with the command line first.
Method 1: Terminate a process using the kill command
To kill a process, you must know its process ID (PID). The following section tells you how to find the process ID of a program.
Step 1: Find the PID of a process
There are several ways for finding the PID of a process.
If you know the name of the process, you can use the command pidof
in this fashion:
pidof program_name
You can take the help of the tab completion to find the program's name. The good thing about this command is that it will give the PIDs of all the processes initiated by the program. Here’s an example:
abhishek@itsfoss:~$ pidof firefox
8980 8871 8821 7228 3796 3742 3741 3590 3381 3355 3085
If the pidof
command doesn’t result in anything, it could mean either there is no process running of that program or the program name you used is incorrect.
You can try the ps command if unaware of the exact program name. 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”
The ps aux
command returns all the running processes on the system. And the grep afterward shows the line which matches 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 immediately:
sudo kill -9 process_id
Here's an example where I terminate a running instance of the Nautilus file manager. As you can see, Nautilus was no longer running after it was killed. The grep --color=auto -i nautilus
is just the grep command running for nautilus search.
If you have more than one process id, you can kill 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 and the pidof
command to kill all the processes 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.
killall
command and kill all processes in one single command killall program_name
Method 2: Kill a process using the top command
The top command is one of the popular commands, which is pre-installed on almost all Linux distributions. It is a powerful task management tool. To use it to kill a process, first open the top by entering:
top
This will open Top and list all the running processes:
Inside the top command, press Shift+L. this will open a “Locate string” prompt, where we can search for the process by name. Once you have located the process that needs to be terminated, press the k key on the keyboard.
Now enter the process ID of the program.
This way, you can terminate any process using the top command. You can also use other system monitoring commands like htop for finding and terminating processes.
Method 3: Killing a process in Linux via GUI
If you are using a Linux desktop, you don't necessarily have to use the terminal.
There is a task manager in every Linux distribution. It's usually called a system monitor.
This system monitor is part of the desktop environment. GNOME, KDE, and Cinnamon, all of them have a system monitor. Just look for them in the system menu.
I'll be demonstrating the GNOME system monitor here.
The default page lists all the running processes. You can kill a process by clicking on it and pressing the End Process button. Or you can right-click on the process and select Kill/End from the context menu.
Also, you can note that the process tab lists the process ID of the respective program.
You may also use various third-party tools like Stacer.
What next?
If you are interested in learning more about this topic, I suggest getting familiar with various termination signals.
The ps command is also quite versatile. Learning its usage will be helpful as well.
So, you learned the command line and GUI methods of terminating a running process in Linux.
The command line method is evergreen and you can use it on any Linux distribution, desktop, or server.
Now it's your turn. Do you prefer the command line method or the GUI tools?