How to use the Kill Command in Linux

This tutorial will show you how to use the kill command in Linux to stop running processes.

kill command on Linux

The kill command is one of the many ways to kill a process on a Linux-based operating system.

This tool is built-in to almost all bourne-based shell systems, the most popular of these being Bash. Additionally, depending on the operating system, it can also be found as a standalone application.

For our guide, we will focus on the version of the kill command implemented as a shell built-in. However, most if not all of the syntax covered here will work regardless.

The most common usage of the kill command is to terminate a running process. However, you can also use it to send other signals such as “SIGHUP” which tells a process to reload.

All you need for this tool to work on your Linux system is the ID belonging to the process you want to send a signal to. To get the ID of a process on Linux, you can use the pidof command.

Over the following few sections, we will be showing you how you can use this command.

Table of Contents

Syntax of the Kill Command on Linux

Before we start showing you how exactly to use the kill command, let us look at its syntax within Linux.

This command has two parameters, the first is optional, and the second is where you would specify the process ID you want to kill.

When you don’t use any options with this command, Linux will kill the specified process ID by using the “SIGTERM” signal.

kill [OPTIONS] PID...

Now that we have seen the syntax of the command let us quickly look at the available parameters.

  • [OPTIONS] – This option is completely optional and allows you to control the behavior of the kill command.

    For example, you can choose precisely what termination signal is utilized when stopping a process.

    Additionally, by using the “-l” option, you can print out both the SIGNUM and SIGSPECs supported by your system.
  • PID – With this option, you will specify the process id that you want the command to terminate. You can specify as many IDs as you want. Just separate them with a single space.

    Some behavioral changes occur when you use a PID that is 0 or -1, which we will cover shortly.

    This option is required unless you use the “-l” or “-L” option to list supported signals.

When using the kill command, it will only give feedback when Linux fails to kill a process. For example, if that process is unavailable, it will provide you with a message such as “No such process“.

The PID Option

Linux’s kill command has some interesting behaviors that may be useful when wanting to terminate processing belonging to the current user or group.

  • If your process ID is positive (Greater than 0), Linux will send the signal to that particular process ID.

    This is the most typical usage of the PID option w
  • If the process ID is zero (Equal to 0), the signal from this command will be sent to all processes belonging to the process group of the current process.

    Using a zero process ID from the shell would mean that this group is likely the current shell’s process group.
  • If the process ID is negative one (Equal to -1), the kill signal will be sent to all processes that the current user has permission to send a signal to.
  • If the process ID is less than negative one (Less than -1), the tool will send the signal to all processes belonging to that particular process group.

    Linux will ignore the minus symbol and interpret the number as a process group ID instead.

Please note, if you are using a negative process ID and are not using any options, you must use “--” in the place of the options. Without doing this, Linux will misinterpret your process ID as an option.

For example, if using a “-1” process ID and using the default signal, you would use the following command.

kill -- -1

Selecting the Kill Signal

The default signal used by the kill command is “SIGTERM“. This signal tells the process to stop gracefully. However, it is far from the only signal that the tool can send.

The kill command on Linux has three ways to specify the option for selecting a signal. These behave the same, but one might be more clear than the other,

  • -<SIGNAL> – You can specify a signal by simply using a hyphen followed immediately by signals ID.

    For example, using this way of specifying a signal, you would use “-1“, “-SIGHUP” or “-HUP“.
  • -s <SIGNAL> – You can also use the “-s” option to specify a signal. With this way of specifying a signal, you need to use “-s“, followed by a space, then finally the signal ID.

    An example of a valid signal set with this option is one of the following: “-s 9“, “-s SIGKILL” or “-s KILL“.
  • --signal <SIGNAL> – This way of defining the signal works exactly the same as the above. Except, you will use the full option name instead and two hyphens.

    Specifying signals using this format can be written as “--signal 15“, “--signal SIGTERM” or “--signal TERM“.

In our examples above, you will notice that we used three different ways of specifying the same signal.

Using the kill Command to Terminate a Process on Linux

Now that we know how the kill command is defined within Linux, let’s explore using it.

The simplest way of using this command is not to utilize any options and simply provide it with a process ID.

Since we wouldn’t be using an option, “SIGTERM” will be used as the termination signal. This signal tells the process to stop gracefully, meaning if the process is unresponsive, this may not stop it.

Below, you can see how this would look without the options field. Remember that you can kill multiple processes at once.

kill PID...

Example of Using the kill Command on a Single Process

For this example, we will be using the kill command to kill a process with the ID “22836“. This process ID belongs to an instance of nano running in the background of our shell.

Remember that pidof is your friend when you need to get the ID of a running process.

Once you have the process ID, all you need to do is write “kill” followed by the process ID, as we have shown below.

kill 22836

If you successfully terminate the process by running the command, you will not see any further messages.

However, if that particular process ID is not running, you will instead see a message as shown below.

-bash: kill: (22836) - No such process

Example of Killing Multiple Processes with the kill Command on Linux

As mentioned before, Linux’s kill command has the full capability to send a terminate signal to multiple processes simultaneously.

For this example, we will be using this tool to terminate two processes, one with the ID 22880 and the other 22881.

Using the command in this way only requires us to specify one ID after the other. Even if one process is not running the kill command will still work on the one that is running.

kill 22880 22881

Listing Linux’s Supported kill Command Signals

The kill command on Linux allows you to easily list all of the termination signals that it natively supports. Being able to list these is useful for quickly referencing the one in particular that you need.

To get the list of supported signals, all you need to do is use the “-l” or “-L” option alongside “kill”.

kill -l

After running the above command, you will see a list of supported termination signals. These are ordered by their signal numbering.

You can use either the sig number (E.G. 1), or the sig spec (E.G. SIGHUP) when specifying a signal.

 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

Most Used Signals

For most people, the default signal used by the kill command will do the job properly. However, there are plenty of other signals that have their uses.

We won’t be going over every signal supported by Linux. Instead, we will be focusing on three of the most used ones.

  • 1, SIGHUP, HUP – This signal tells the process that the user’s session has ended and the process should be terminated.

    Depending on the process’s implementation, this may instead cause a restarting of the process. Typically, this behavior is seen in non-interactive programs where a user session is meaningless.
  • 9, SIGKILL, KILL – You can kill a process using this signal. However, unlike “SIGTERM“, this will not allow a process to shutdown gracefully.

    It will cause the process to stop immediately. This signal cannot be blocked or handled. It should only ever be used as an absolute last resort.
  • 15, SIGTERM, TERM – TERM is the default signal utilized by the kill command on Linux and is typically the one you will use the most.

    This signal tells the process that it should shutdown gracefully. It gives time for a process to complete any tasks it needs to finish before safely stopping.

Setting a Signal for the Linux kill Command

There are three ways to set a signal for the kill command and override the standard “SIGTERM” signal. For this example, we will be focusing on utilizing the “-s” option, but all methods operate the same.

You can view all the supported ways of defining a custom signal in the “Selecting the Kill Signal” section.

Additionally, you can use the “-l” option to list all of the supported signals.

Below, you can see the basic syntax for setting the signal for the kill command to use on Linux.

kill -s SIGNAL PID

Example of Using the “SIGHUP” Signal for the kill Command

For this example, we will be using the kill command to send the “SIGHUP” signal on a processor with the ID “28367“.

You can specify the SIGHUP signal by using either “1“, “SIGHUP” or “HUP” after the “-s” option.

Below you can see three commands that will send the same SIGHUP signal to our specified process but using slightly different formats.

This first command uses the full signal name.

kill -s SIGHUP 28367

The second command will utilize the signal’s number.

kill -s 1 28367

The final command uses the shortened version of the signal name, dropping off the “SIG” part of the name.

kill -s HUP 28367

Conclusion

By the end of this tutorial, you should now understand how you can utilize the kill command in Linux.

This command allows you to terminate any process that is currently running on your system. The default signal won’t force close a process, but using a signal like “SIGTERM” can override this behavior.

If you have any issues using the kill command on Linux, please comment below.

To learn more about Linux, check out our many other Linux command guides.

Leave a Reply

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