Guide to Kill, Pkill and Killall Commands to Terminate a Process

Linux Operating System comes with Kill command to terminate a process. The command makes it possible to continue running the server without the need of reboot after a major change/update. Here comes the great power of Linux and this is one of the reasons, why Linux is running on 90% of servers, on the planet.

Kill command send a signal, a specified signal to be more perfect to a process. The kill command can be executed in a number of ways, directly or from a shell script.

Using kill command from /usr/bin provide you some extra feature to kill a process by process name using pkill. The common syntax for kill command is:

For a kill command a Signal Name could be:

Clearly from the behaviour above SIGTERM is the default and safest way to kill a process. SIGHUP is less secure way of killing a process as SIGTERM. SIGKILL is the most unsafe way among the above three, to kill a process which terminates a process without saving.

In order to kill a process, we need to know the Process ID of a process. A Process is an instance of a program. Every-time a program starts, automatically an unique PID is generated for that process. Every Process in Linux, have a pid. The first process that starts when Linux System is booted is – init process, hence it is assigned a value of ‘1‘ in most of the cases.

Init is the master process and can not be killed this way, which insures that the master process don’t gets killed accidentally. Init decides and allows itself to be killed, where kill is merely a request for a shutdown.

To know all the processes and correspondingly their assigned pid, run.

Sample Output

How about Customising the above output using syntax as ‘pidof process‘.

Sample Output

Another way to achieve the above goal is to follow the below syntax.

Sample Output

Before we step ahead and execute a kill command, some important points to be noted:

  1. A user can kill all his process.
  2. A user can not kill another user’s process.
  3. A user can not kill processes System is using.
  4. A root user can kill System-level-process and the process of any user.

Another way to perform the same function is to execute ‘pgrep‘ command.

Sample Output

To kill the above process PID, use the kill command as shown.

The above command will kill the process having pid=3139, where PID is a Numerical Value of process.

Another way to perform the same function, can be rewritten as.

Similarly ‘kill -9 PID‘ is similar to ‘kill -SIGKILL PID‘ and vice-versa.

How about killing a process using process name

You must be aware of process name, before killing and entering a wrong process name may screw you.

Kill more than one process at a time.

What if a process have too many instances and a number of child processes, we have a command ‘killall‘. This is the only command of this family, which takes process name as argument in-place of process number.

Syntax:

To kill all mysql instances along with child processes, use the command as follow.

You can always verify the status of the process if it is running or not, using any of the below command.