How to Kill Process in Linux

Linux comes with a kill command to terminate the processes. The command is located either in a shell or externally. You can use this command to kill a process in Linux without having to restart or log out of the system.

Suppose you are working on an application and it suddenly stops working or unexpectedly dies. After that, what will you do? You will try to rerun the application, but the original one has not shut down yet. In this situation, you will try to kill the process through the command line. But before you begin with this step, it is crucial to know the underlying layers of processes and how they work.

process in linux

This tutorial will cover how to kill a process in Linux using the Command-line interface. It will also explain how to verify that the terminated process is no longer running in the background. So, open your CLI and get ready to type.

What is a Process ID?

Before you begin with the termination of the process, it is essential to know the process ID. In Linux, a process ID is a unique process identification number assigned to each process. 

In addition, the operating system assigns PID to each process. Furthermore, you can find out the PID of any process or command by using the pidof command. Some examples of pidof command is: 

pidof httpd
pidof apache2
pidof firefox

You can also use the pgrep command to find out the process ids of any process. To do this, you can execute the commands given below:

pgrep process
pgrep firefox
pgrep vim

1. Locate the Process

Before you kill any process, you will have to locate it first. In addition, there are two commands to locate the process in CLI: top and ps. With the top command, you can see the list of all the processes that are running currently. Also, it is an interactive list. Furthermore, you can go through the list to find out the PID of the processes you want to kill.

view process

For instance, suppose Firefox is not responding. By using the top command, you can locate the processes and their process IDs and in the PID column. The PID is a piece of important information for you when you might want to kill the process. 

Sometimes, the top command is not that useful as it gives a comprehensive list of information. In that case, you can use the ps command with the grep filter. In contrast, the ps command will give you a snapshot of the current process. Furthermore, the grep filter will filter out the particular process. In the case of Firefox, you will run the ps command as shown below.

ps aux | grep firefox
view process using ps

Here, aux stands for:

  • A = list all processes
  • U = list all processes’ owner
  • X = list all the processes not attached to the terminal

2. Kill the Process

Now that you have located the process name and ID by using the top or ps command, it is time to kill it.  You can use two commands to kill the process: kill and killall. The kill command requires process ID, while the killall command requires process name. But before that, you need to know what processes you can kill.

Kill the Process Using Kill Command

If you are a local user, you can kill your processes only. You cannot kill the processes of the other users. However, as a root user, you can kill all the processes. You can use the sudo command to have the root user privilege. 

The syntax for the kill command is shown below:

kill -[signal] PID
kill -15 PID
kill -9 PID
kill -SIGTERM PID
kill [options] -SIGTERM PID

The kill command sends specified signals to the processes. However, if you have not stated the signal, it will send a TERM signal to the specified process. There are several signals that you can also use with the kill command to get different results. 

If you want to view the signals, type the command given below, and you will get the list of signals that you can use with the kill command:

kill -l
kill process in linux

The most common signals are:

  • SIGHUP
  • SIGINT
  • SIGKILL
  • SIGTERM
  • SIGSTOP

You can also use signal IDs instead of signal names. The table given below shows the list of signal names with their IDs and their purpose.

Signal NameSignal ValueEffect
SIGHUP1Hang up an uncontrollable process. This signal will open or close log files and reload config files.
SIGINT2Send interrupts from the keyboard.
SIGKILL9Kill signal. It will not save any data of the process.
SIGTERM15Termination signal. It is the safest way to kill any process.
SIGSTOP17, 19, 23Stop the process

Finally, it is time to kill the Firefox process. We already know Process IDs from the ps command. Let’s utilize those to kill the processes. You can kill each process separately by issuing the following commands:

kill -9 3827
kill -9 3919

Once the commands are executed, all the processes related to Firefox are killed. 

Kill the Process Using Killall Command

Alternatively, you can also use the command given below to kill all the processes related to Firefox.

killall -9 firefox

The killall is a Linux-only command, and it does not require process IDs. You can directly use it as shown in the syntax given below:

killall {Process-Name-Here}
killall -9 {Process-Name-Here}
killall -15 {Process-Name-Here}
kill process in Linux

Additionally, with the killall command, you can also kill the processes that have been running for a certain time. The syntax below kills any process that has been running for 30 minutes.

killall -o 30m <process-name>

For the specific unit of time, you can use the following abbreviations.

  • s: seconds
  • m: minutes
  • h: hours
  • d: days
  • w: weeks
  • m: months
  • y: years

3. Verify the Killed Process

Moreover, you can verify that you have killed the process. To verify that you have terminated the specific process, use the pidof or pgrep command.

pidof firefox
pgrep firefox

Lastly, you will not get any output if you have successfully killed the process.

In conclusion, this tutorial has covered how to kill a process in Linux in three simple steps. Also, the process is relatively simple and requires no complex installations or setups. You can also check the manual of the kill command by using the man command. 

If you liked this post, please share it with your friends. In case of any suggestions or queries, leave a comment below.

If this guide helped you, please share it.

Leave a Reply
Related Posts