How to Kill a Process on Linux

This guide will show you how to kill a process that is running on a Linux system.

kill process on linux

Knowing how to kill a process on a Linux system is an important thing to learn. It can help you deal with a variety of problems, such as stopping an unresponsive application.

It is also helpful for stopping applications that aren’t active in your current terminal session or on your desktop interface.

These steps for killing processes on Linux should work for all distributions including, CentOS, Debian, and Ubuntu.

There are three different tools that we will be exploring that you can use to kill a process on Linux. These tools are “kill“, “killall“, and “pkill“.

Each of these tools has slight differentiations in how they function. For example, the “kill” command requires the processes ID number (PID), but the “pkill” command can kill a process based on its name.

You don’t have to be a superuser to kill a process on Linux. If you are the user who started that process, you can kill it without elevating your privileges.

Linux Kill Signals

All of the commands that we will be showing today work by sending a kill signal to the process. There are a variety of signals that the Linux system uses to end a process.

When you don’t specify a signal, the “kill“, “killall“, and “pkill” commands will send signal 15 (SIGTERM) to the process.

There are two signals in particular that you will most likely use when killing a process.

  • -SIGKILL (Code 9) – Using “SIGKILL” ensures that the system kills the process. The Linux Kernel will not wait for the process to stop safely and will terminate it immediately.

  • -SIGTERM (Code 15) -The “SIGTERM” signal attempts to kill the specified process safely. There is no guarantee the process will be killed when using this, and it is possible to block a “SIGTERM” signal.

If you want to see the other signals you can send when killing a process, you can run the following command.

kill -l

Below is a screenshot of the process kill signals available to us on our Ubuntu system.

Linux List Kill Process Signals

You can pass these signals through to the kill commands in a variety of ways.

  • You can use the numerical reference for the signal. Refer to the screenshot above as a reference. When using the signal number, use a hyphen (-) followed by the number. For example, “-15” will send a “SIGTERM“.

kill -TERM 1111
  • You can also use the signal events name prefixed with “SIG“. For example, if we wanted to send a SIGTERM event, we would use a hyphen (-) followed by “SIGTERM“.

kill -SIGTERM 1111
  • When using the signal event name, you don’t need to include the prefix. If we only want to send a SIGTERM event, we only need to use the “TERM” part of the name. For example, we would use a hyphen (-) followed by “TERM” to send the terminate signal.

kill -TERM 1111

Now that you have an idea of some of the process kill signals that Linux supports, we can move on to showing you how to use them.

Stopping a Process Using the kill Command

The first command for terminating a process on Linux we are going to show you is the “kill” command.

Below we have included the basic syntax for using the kill command on your Linux system.

kill [OPTION] PROCESSIDS...

This command requires you to know the ID of the processes you want to kill.

There are various ways you can get this ID, as it is shown in several places, including the results produced by the top command.

Example of Using the kill Command on Linux

1. For this section, we will be using the pidof command to get the IDs of the processes that we want to terminate.

All we need to do is pass in the name of the process we want to get the IDs of. pidof will find any processes with the same name and return all of their IDs.

For this example, we will want to get all of the IDs of the NGINX process running on our Linux device.

pidof nginx

2. Below is an example of the IDs you will get from using the “pidof” command.

You will use these IDs to terminate the running NGINX processes in the next step.

4128 4127

3. Now that we know the ID of the processes we want to kill on our Linux system, we can proceed.

As we want to terminate these processes immediately, we will use the kill command, followed by the -SIGKILL option, then the IDs we want to terminate.

kill -SIGKILL 4128 4127

Terminating Processes Using the pkill Command

If you want to skip the step of getting the ID of the process you want to kill, it is possible to use the “pkill” command.

Unlike the kill command, the pkill command allows you to provide the name of the process you want to terminate on your Linux system.

pkill [OPTIONS] PROCESS_NAME

There are a few other benefits to using the pkill command, such as that you can also specify the user that the process belongs to that you want to kill.

Below is a list of some of the options that you can use alongside the pkill command.

  • -u USERNAME (--user USERNAME) – Only match against processes owned by the specified user.
  • -x (--exact) – Ensure that the process name exactly matches the specified pattern.
  • -n (--newer) – Kill the newest process that was discovered when searching.
  • -u (--older) – Kill the oldest process that matches the specified pattern.
  • -signal – This allows you to specify the signal that’s sent to the process. Replace “signal” with the signal number or name.

Example of Using pkill on a Process

1. For our first example, let us say that we wanted to kill the NGINX process running on our system.

All we would need to do is use the following command, with pkill followed by “nginx“.

Additionally, if we want to perform a KILL on that process, we would include “-SIGKILL” as the option.

pkill -SIGTERM nginx

The pkill command will now search for a process called nginx and kill it.

Please note that this command will only terminate the first occurrence if there are multiple instances of the process.

2. The pkill command has some additional options, such as only killing the process belonging to a specified user.

For example, if we only wanted to kill the nginx process belonging to our pimylifeup user, we can use the “-u” option.

pkill -SIGTERM -u pimylifeup nginx

Terminating Processes Using the killall Command

The last command that we will be going through in this guide is the “killall” command.

This command differs in that it is designed to terminate all processes that match the specified options. This is useful when you need to terminate multiple processes at once.

The basic syntax for the “killall” command is the following.

killall [OPTIONS] PATTERN

For the most basic usage, all you need to know is the name of the process you want to kill.

However, the killall command does have several options that you can use to configure its behavior, as shown below.

  • -e (--ignore-case) – Only find exact matches for the process name that has been specified.
  • -I (--ignore-case) – Ignore case mismatches when searching for a process by name.
  • -i (--interactive) – Requests the user’s confirmation before killing the found process.
  • -u USERNAME (--user USERNAME) – Only kill processes if it belongs to the specified user.
  • -v (--verbose) – Report whether the process kill signal was successful or not.
  • -signal – Specify the signal to send when killing the process. Ensure that you swap out “signal” with the number or name of the signal you want to send.

Example of using killall Using Process Name

Using the killall command is relatively straightforward. All you need to know is the name of the process.

1. For our first example, let us terminate all NGINX processes running on our Linux system.

As we want these processes to be stopped immediately, we also specify the signal as “SIGKILL“.

killall -SIGKILL nginx

2. If you only wanted to kill all of the NGINX processes that a specific user started, you can use the -u option.

For example, let us say that we had a user called “pimylifeup” that started up lots of NGINX processes we want to terminate.

We can kill all of these processes on our Linux system by running the killall command like below.

killall -SIGKILL -u pimylifeup nginx

3. It is also possible to use killall to terminate all processes belonging to a user.

This is useful in cases where you need to stop everything from being run by a specific user. All you need to do is include “-u” option followed by the user’s name.

For example, to kill all processes that our “pimylifeup” user owns we can use the command below.

killall -SIGKILL -u pimylifeup

Performing a killall Based on Process Age

The killall command has some additional functionality that allows you to terminate processes based on their age. This means you can kill off old or newly started processes.

To do this, there are two separate options that you can use alongside the killall command.

  • -o TIME (--holder-than TIME) – Only match processes that were after before the time specified.
  • -y TIME (--younger-than TIME) – Tells killall only to match processes that were started before the time that has been specified

The time is specified as a duration represented as a number followed by a unit for these options.

You can see a list of the units supported by the killall command below.

  • s – Seconds
  • m – Minutes
  • h – Hours
  • d – Days
  • w – Weeks
  • M – Months
  • y – Years

For example, if we wanted to kill all NGINX processes that were opened in the last 15 minutes on our Linux system, we can use the following command.

killall -y 15m nginx

Likewise, if we wanted to terminate NGINX processes that were opened over 1 year ago, we can use the command as we have below.

killall -o 1y nginx

Conclusion

Hopefully, this tutorial has shown you how you can kill a process on your Linux system.

You can use various tools to achieve this, and we have touched on three of the most powerful ones available on Linux.

We have also quickly explained that there are a variety of different signals that you can send to terminate a process.

Two of the most important signals to use are “SIGKILL” which terminates a process immediately, and “SIGTERM” which allows a process to shut down safely.

Please note that if you are having issues with killing a process, please leave a comment below.

You can also check out some of our other Linux tutorials and Linux command guides.

Leave a Reply

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