Killing a process under Linux: kill, killall and pkill

3 commands to kill a process under Linux

Just imagine, you've had a great day working on your Linux machine, and suddenly a process starts slowing down the whole system. You've identified it and want to stop it, but how do you kill a command line process in Linux?

If the process is running in the foreground, you can use the terminal shortcut "Ctrl+C" to stop it directly. However, if the process is not visible because it's running in the background, and you've spotted it after executing the "top" command, for example, you can use dedicated commands to "kill" it.

In fact, the term kill a process "This refers to the stopping of a running process. If you know the process ID (PID), you can use the kill command as follows:

kill

In the above command syntax, the "signal" block refers to the stop signal you wish to send to terminate the process, and the "PID" block refers to the process ID.

There are other alternative commands to the kill command, so in this article I'll look at several solutions for killing a process, including the killall command.

1. Signals in Linux

First, let's take a closer look at the signals at the end of the Linux. When a process is terminated by the operating system or by the user, i.e. when the process has not terminated naturally (on its own), it receives a termination signal from the system.

Here are the available signals:

  • SIGHUP – Signal Hangup

Sent to a process when the terminal controlling it is closed.

Associated with the numerical value "1".

  • SIGINT – Signal Interrupt

The signal sent to a process when a user terminates a process (e.g., Ctrl + C)

Associated with the numerical value "2".

  • SIGKILL – Signal Kill

The signal that immediately exits a process, without allowing it to save its current state.

Associated with the numerical value "9".

  • SIGTERM – Signal Terminate

Signal sent to request the end of the process. This signal can be ignored by a process, but it is the best way to terminate a process as it can free up resources when the process receives SIGTERM.

Associated with the numerical value "15".

  • SIGSTOP – Signal Stop

Signal used to stop a process, but to be resumed at a later time.

Associated with the numerical value "19" on most machines, or other values on specific machines (17, 23 and 24).

In practice, you're likely to use signals 9 and 15. Before moving on to the use of commands, let's see how to obtain the PID of a process.

2. Obtain the PID of a process

You also need to know a minimum amount of information about the process you wish to kill. How can you kill a process if it hasn't been identified beforehand? Difficult, you may say, especially as the kill command requires you to provide the process identifier (PID).

You can obtain the PID from the process name:

pidof

3. Killing a process on the Linux command line

Let's take a look at the kill command first, as you'll be using it more than the killall command, which is still interesting in spite of everything.

A. Using the kill command

The kill command requires you to know the ID of a process you want to kill and, if applicable, the end signal.

To simply kill a process, use the following syntax:

kill

Sending an end signal to a PID is optional, and if no signal is supplied, kill sends SIGTERM (15) by default.. In other words, you can use this syntax :

kill

Let's take an example... I've launched a background process with the sleep command to wait 120 seconds, and running it in the background to obtain the PID (&). We'll try to kill this process.

sleep 120 &
[1] 46532

Given the return of the above command, I know that my process has the ID "46532", so to kill it, I have to do :

kill 46532
Kill a Linux process with the kill command

If I wanted to use a specific end signal to terminate this process, I could use either the numerical value corresponding to this signal, or the name of the signal directly. Here's an example along the same lines as the previous one:

sleep 120 &
[1] 46532

This gives kill the process :

kill -SIGKILL 46532

If the numerical value is used, this syntax must be used:

kill -9 46532

It's not always useful to specify a signal type, because by default, if you don't specify anything, the kill command will use SIGTERM (15).

B. Using the killall command

If you don't know the PID of a process, or if that process has several children, and you want to kill child processes and the parent process at the same timeyou can use the killall command. It is used on a similar principle to the kill command, the main command for kill a Linux process :

killall

As with the kill command, specifying an end signal is optional. When no termination signal is specified, killall will send SIGTERM (15) to stop the process in question, just as the kill command does.

Let's take the same example as before, based on "sleep", except that this time I want to kill two sleep commands at runtime.

$ sleep 60 &
[1] 46536
$ sleep 120 &
[2] 46537

To kill these two "sleep" processes with killall, this gives :

$ killall sleep
[1]- Completed sleep 60
[2]+ Finished sleep 120

Here's how it looks with the same example:

Linux kill a process with killall

C. Using the pkill command

An alternative to kill is the pkill command, which is also capable of kill a Linux process. In fact, it's a sort of combination of the pgrep and kill commands. The killall command kills all processes with a matching name. On the other hand, pkill uses the matching principle via patterns to match processes and kill them.

The advantage of the pkill command is that it kills a process simply by its name, instead of the PID.

Here's the syntax:

pkill  pattern

Some useful options available in the pkill command are :

  • -u : processus appartenant à un propriétaire particulier
  • -x : processus qui correspondent exactement au pattern
  • -signal : spécifier un signal de terminaison (par défaut SIGTERM)

This pattern can simply be the name of the process, which would give the command below, if we take the previous example.

pkill sleep

Since pkill is based on the matching principle, the command below also kills the "sleep" process:

pkill slee

The pkill command is interesting, but should be used with care!

4. Conclusion

With this article from the Computer Tutorials box, you have learned 3 different commands to kill a process under Linux. Personally, I mostly use the "kill" command, or possibly "killall".

Resources :

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *