Log In

How to Kill a Process in Linux

How to Kill a Process in Linux
20.12.2023
Reading time: 5 min
Hostman Team
Technical writer

In the Linux operating system, the ability to kill or terminate processes is a crucial and powerful feature that serves various purposes in managing and maintaining a system's performance, stability, and security. Killing a process is essentially a way to stop a running program or task. 

Linux users, system administrators, and developers can all benefit from knowing how and when to stop processes. It enables efficient system performance management, problem solving, and the maintenance of system stability and security. Given that the improper strategy could have unforeseen repercussions, it is crucial to use caution when killing processes and select the right signal (such as SIGTERM or SIGKILL) depending on the situation.

How to Locate and Kill a Process in Linux

To locate a process that you want to terminate in Linux, you can use various commands and techniques. Here's how to do it:

You can terminate a process in Linux using a variety of commands and methods, depending on your needs and the situation. Here are a few popular methods for terminating a process:

  1. Use the ps Command

The ps command can list running processes. You can filter and search for a specific process using grep. Here’s the basic syntax: 

root@stressfree-websites:~# ps aux | grep postfix
root     1538975  0.0  0.4  41964  4720 ?        Ss   09:00   0:00 /usr/lib/postfix/sbin/master -w
postfix  1538977  0.0  0.7  42312  7052 ?        S    09:00   0:00 pickup -l -t unix -u -c
postfix  1538978  0.0  0.7  42356  7096 ?        S    09:00   0:00 qmgr -l -t unix -u
root     1539517  0.0  0.2   6852  2132 pts/1    S+   10:30   0:00 grep --color=auto postfix
  1. Using pgrep Command

pgrep is a dedicated command for finding the PID of a process based on its name. The basic syntax is:

pgrep <process_name>
  1. Using htop or top (Interactive Process Viewer)

The htop or top command provides an interactive process viewer. Run either of these commands, and you can search for processes, sort them, and even send signals to them directly from the interface. Here's how to use htop

root@stressfree-websites:~# htop

Image4

To search for a specific process in htop, press the "F3" key and type the process name. Then, select the process and press the "F9" key to send a signal (e.g., SIGTERM) to it.

  1.  Using System Monitor (Graphical Interface)

Numerous Linux distributions provide a graphical task manager or system monitor. It makes it simple to observe and manage active processes. Usually, you can find it by looking for "System Monitor" or "Task Manager" in the system menu.

  1. Using pidof Command (for Processes with Known Names)

You can use the pidof Command if you know the name of the process you want to terminate and it's a command that's directly executable 

Once you've located the Process ID (PID) of the process you want to terminate, you can use the kill command with the appropriate signal to terminate the process.

Different Commands for Killing a Process

  1. Using the kill Command

The kill command is a general way to send signals to processes. The most commonly used signal is SIGTERM (15), which politely asks the process to terminate. 

Below is unix machine with hostname “stressfree-websites”. 

For example, to kill a process postfix with a process ID (PID) of 44259:

  • Check and verify if postfix is running via below command:
ps -ef | grep postfix

PID is showing 44259:

Image3

If the process is integrated with system, you can check the status of it using systemctl status  <servicename>.

systemctl status postfix.service

Image6

  • Given all details gathered on above, it is now ready to kill the process.
kill <PID>

Image5

  • Verify if postfix is still running:
ps -ef | grep postfix | grep -v grep | wc -l

Image8

Postfix process with PID 44259 successfully killed. 0 meaning no process is running.

  1. Using the killall Command

The killall command allows you to kill processes by name rather than PID. Be careful when using this command, as it can terminate multiple processes with the same name.

 killall <process_name>

For example, to kill all processes named "postfix":

  • You can get the process name on the column 9 of the output below:
ps -ef | grep postfix

The process name is /usr/lib/postfix/sbin/master.

Image7

  • Once process name identified, you can kill the process now.
killall /usr/lib/postfix/sbin/master

Image10

  • Verify if postfix is still running:
ps -ef | grep postfix | grep -v grep | wc -l

Image8

Postfix process successfully killed. 0 meaning no process is running.

  1. Using pkill Command

pkill is another command to send signals to processes based on their names or other attributes. 

For example, to kill all processes whose name contains "postfix":

  • Use this command:
pkill -f postfix

Image1

  • Verify if postfix is still running:
ps -ef | grep postfix | grep -v grep | wc -l

Image8

Postfix process successfully killed. 0 meaning no process is running.

  1. Using kill with Signal Options

You can send different signals to a process depending on the situation. The default is SIGTERM, but you can use other signals like SIGKILL (9), which forcefully terminates the process.

  • Check and verify if postfix is running via below command.
ps -ef | grep postfix

Image12

PID is showing 1491887.

  • To forcefully kill a process with PID 1491887:
kill -9 <PID>

Image13

  • Verify if postfix is still running:
ps -ef | grep postfix | grep -v grep | wc -l

Image8

Postfix process with PID 44259 successfully killed. 0 meaning no process is running.

Remember that killing a process with SIGKILL (signal 9) should only be used as a last resort, as it does not allow the process to clear up resources and may result in data corruption in some instances. It is preferable to use SIGTERM first and SIGKILL only if necessary.


Share