How to kill processes in Linux

Here we will see how to kill processes in Linux. We will use kill, killall and pkill. Let see how to use this. Read more : Kill all instances of httpd

The main difference between these tools is that kill terminates processes based on Process ID number (PID), while the killall and pkill commands terminate running processes based on their names and other attributes.

System Kill Signals

Signals are a form of a dialogue between the processes that can come from other processes, the kernel, or the process itself. These kill, killall, and pkill commands send a given signal to the mentioned processes or process groups. If there are no signals provided, each tool sends 15 (TERM).

Few of the commons signals used are as follows.

  • 1 (-HUP): to reload a process.
  • 9 (-KILL): to kill a process.
  • 15 (-TERM): to gracefully stop a process.

Signals can be specified in three different ways

  • using a number (e.g., -1)
  • with the “SIG” prefix (e.g., -SIGHUP
  • without the “SIG” prefix (e.g., -HUP).

1. Kill Command – Kill the process by specifying its PID

The common syntax for kill command is:

# kill [signal or option] PID(s)

However, to terminate a process with the kill command first, you need to find the process PID. The simplest way to find this is either using commands like top, ps, pgrep or pidof.

2. killall Command – kill the processes by name. By default, it will send a TERM signal. The killall command can kill multiple processes with a single command. If more than one process runs with that name, all of them will be killed.

# killall [process]

Example: Kill all the firefox processes

killall -9 httpd

3. Pkill Command – Send signal to the process based on its name. You can send signal to any process by specifying the full name or partial name. So there is no need for you to find out the PID of the process to send the signal.

pkill [options] pattern

Simply, type pkill with the process name that you want to kill as input. You must be aware of the process name before killing and entering a wrong process name may screw you.

Example: Kill all the MySQL processes

pkill httpd

Leave a Reply