Linux Commands Ubuntu

How to kill a process in Linux/Ubuntu by PID?

How to kill a process in Linux Ubuntu by PID

A Process in Ubuntu is simply any program or task that is running. It may be running in the background or you might be using that program. A Process may be a System Process or a User Process. The System Processes run on the kernel level. On the other hand, the User Processes are those that are run by the Users, they consist of a web browser, a music player, and many more applications. Each process has a Process ID.

In this article, we will cover different methods of killing a process using the Process ID or PID of that application.

How to kill a Process in Linux/Ubuntu by PID?

Killing a process means terminating the process or closing the process. There are mainly two commands used to kill a Process based on its Process ID or PID, the two commands are:

  • kill” Command
  • killall” Command

Both these commands have their method of killing a process which is explained below.

Using SIGTERM vs. SIGKILL for Killing Process

Processes can be killed either gracefully or forcefully. Two types of signals can be added to the command. One Signal is the SIGTERM and the other is the SIGKILL. The SIGTERM terminates a process gracefully whereas the SIGKILL terminates a process forcefully or immediately. In the SIGSTREAM, the process will save the data and then do a clean shutdown. In the SIGKILL, the process may or may not save the data and thus it should be avoided. SIGTERM uses the signal “-15” whereas SIGKILL uses the signal “-9”.

See the Running Processes

To see the list of running processes on your Ubuntu system, you can use the “ps -ef” command:

ps -ef

This will list all the processes with Process IDs that are currently running on your system:

Killing a Process in Ubuntu Using “kill” Command

The “kill” command is used to kill a specific Process ID running on your Ubuntu System. To kill a Process ID with the “kill” command you can use the “kill” command with the Process ID mentioned:

kill <pid>

In our case, if we want to kill a Process ID of the Firefox Web Application, we have to get the running process ID of Firefox first using the “pidof” Command. The “pidof” Command is used to list the running processes of a Specific Application. To display the process IDs of Firefox:

pidof firefox

All the running Firefox Processes IDs will be displayed in the terminal:

To Kill the Process ID or PID “4469”, use the “kill” command:

kill 4469

Once killed, you can verify the process IDs using the “pidof” command which will list all the Process IDs again:

pidof firefox

You will see the Process ID “4469” is killed by comparing previous process IDs with the new ones.

Killing a Process in Ubuntu Using the “killall” Command

The “killall” command will kill all of the processes that are currently run by an application. The “killall” command is executed with the name of the Application you want to kill the process of. The syntax of “killall” command is

killall <application-name>

Considering our case where we want to kill all the processes of the Firefox browser. Before killing all the processes, use the “pidof” command to list the processes of Firefox:

pidof firefox

This will display all the running processes of Firefox:

To kill all processes of firefox, use the “killall” command with the name of an application, in our case, Firefox:

killall firefox

This will kill all the processes of Firefox and will close the application as well:

Once the processes are killed, we use verify using the “pidof” Command again:

pidof firefox

As no Process IDs are shown and the command line moves to the next line means all of the processes of Firefox are closed:

Killing a Process in Ubuntu Using SIGTERM

The SIGTERM can be used with the “kill” command to kill the process in a more clean way. Before killing the process, the process saves the data of that process and performs a clean shutdown. The SIGTERM signal is used with the kill command:

kill -15 <PID>

Considering our case, we have a Rhythmbox application opened in our Ubuntu system.

To check the process ID of Rhythymbox, use the “pidof” command:

pidof rhythmbox

The rhythmbox has only one Process ID “3135”:

To kill the process using SIGTERM, use the command:

kill -15 3135

This will kill the Process by waiting for the process to save data and then closing application:

To verify, the “pidof” command can be used again:

pidof rhythmbox

The Command Line moves to the next line without displaying the PID meaning the application is closed.

Killing a Process in Ubuntu Using SIGKILL

The SIGKILL can be used with the “kill” command to kill the process forcefully. The SIGKILL is usually avoided as before forcefully killing a process, the process may or may not save the data. The SIGKILL signal is used with the kill command:

kill -9 <PID>

Considering our previous case where the Rhythmbox application was opened in our Ubuntu system:

To list the process ID of Rhythmbox using the “pidof” Command:

pidof rhythmbox

The Rhythmbox has a process ID “3288”:

To kill this Process using the SIGKILL use the “kill” command with the SIGKILL Process Signal:

kill -9 3288

The Process will be closed forcefully and the application will close:

To verify if the process was killed, use the “pidof” Command again:

pidof rhythmbox

The command line will move to the next line without displaying the Process ID means the Process was killed using the Kill command:

Killing a Process in Ubuntu at a Specified Time

The “kill” process can also kill a process that has been in execution for some time on Ubuntu. For instance, when killing a Process that has been in execution for more than 24 hours, use the command:

killall -o 24h <app-name>

The “-o” in the command specifies the processes to be killed. All the processes even those owned by other users will be killed. The “24h” is the Time Constraint. Consider our previous case where we have Firefox processes:

To Kill the processes that have been running for more than 2 minutes, the “killall” command can be used with the time mentioned in the command:

killall -o 2m firefox

As all the processes were running for more than 2 minutes, all the processes will be killed:

The killed processes can be verified with the “pidof” command. The command line moves to the next line meaning none of the processes are running:

Similarly, instead of minutes, hours and seconds can also be mentioned.

Conclusion

To terminate a Process using its Process ID, the “kill” Command, as well as the “killall” Command, can be used. The “kill” Command terminates one process at a time whereas the “killall” command terminates all the processes of the application and closes it. In this article, we have covered both methods to terminate processes based on their Process IDs and we have also mentioned the SIGTERM vs. SIGKILL way of terminating a Process.

Similar Posts