Managing Linux processes is one of the skills that any Linux user or sysadmin should have at their fingertips. Part of this skill set involves killing undesirable processes.
Basically, 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.
In this context, we shall look into how you can find and kill a running process on your Ubuntu Linux system.
What is a Process in Linux ?
A process is an instance of a running application or command. The Linux operating system tracks a process using a PID (Process ID). A PID is unique to a process.
Types of processes
Fundamentally, Linux processes can be classified into two: Foreground and background processes. Let's briefly go over each of these.
i. Foreground processes
These are synonymously known as interactive processes. A foreground process is one that is initialized or spawned using a terminal by a logged-in user. Simply out, foreground processes are started by users when they input commands on the terminal shell. They do not start automatically.
ii. Background processes
Background processes are quite the opposite of foreground processes. They are referred to as non-interactive processes and require no keyboard input. In short, no user intervention is required to start a background process.
How to kill a process in Linux ?
Sometimes, you may need to kill or end unresponsive processes or processes which are gobbling up system resources. The best approach is to use the terminal which comes with an array of command-line tools to help you find and kill unwanted processes.
When you launch an application, some processes are initiated in the background. If the application becomes unresponsive or slow for one reason or the other, you can forcefully close it by killing the process associated or linked to it.
Before you kill a process, you must first identify its PID or Process ID.
They are many ways of obtaining the PID of a process. Let's explore.
1. How to find the PID of a process or program
One of the easiest ways of unearthing the PID of a process is using the pidof command-line tool.
If you already know the program's name, then use the command syntax below to obtain its PID:
$ pidof program_name
For example, to find out all the processes initiated by the 'Firefox' browser, run the command:
$ pidof firefox
If you are not certain of the name of the program, run the Linux ps command as shown:
$ ps -aux | grep -i application_name
Now let's see how you can terminate the processes using the PID.
2. How to kill a running process using the PID ?
Execute the command below to kill a running process with a PID:
$ sudo kill -9 PID
If the program has multiple running processes, simply list all the PIDs in one line as follows.
$ sudo kill -9 PID_1 PID_2 PID_3
In our example, this translates to
$ sudo kill -9 10528 10220 10460 10437 10406
This approach can be a bit tedious and time-consuming.
A better way of killing all processes is by using the awesome killall command followed by the name of the application.
This terminates all processes associated with the program:
$ sudo killall name_of_program
In our case to kill all processes associated with the Firefox browser, execute the command:
$ sudo killall firefox