Linux Kill Process - Kill Command In Linux

Do you know how to kill a process in Linux OS? This article will guide you to with Linux Kill process command with examples.

Updated: 01 Dec, 24 by Susith Nonis 14 Min

List of content you will read in this article:

Despite being the most versatile operating system, Linux occasionally experiences application failures that disrupt system operation. These applications become unresponsive to any action. As a result, killing or terminating that running application or process is required to restore the application's proper operation. To resolve this issue, use the kill command in Linux to terminate that process, or you will be aware of the Linux Kill Process.

This article will focus on the kill command and the various options that can be used.

You may have experienced this when a running program was unresponsive or caused other problems. In this situation, you need to stop that running program. That’s where exactly the "kill process Linux" command comes in handy. This command is used in Linux and other Unix-based systems to stop programs. Using this command, you’ll send a message to the operating system to stop that program. 

How does it work?

Just like every person has a social security number, every program in your system has a specific ID number that helps the computer keep track of it. When we want to kill a process, we use this ID number of the running program and tell the OS to stop it. 

Why Do We Need to Stop a Program?

There are many reasons for closing running programs using the kill process Linux. Some of the most common reasons include:

  1. Unresponsive Programs: You have noticed sometimes programs freeze or stop responding. In this situation, you can’t close the program like you normally do. So, you can use the Linux kill process command to forcefully stop the program.
  2. Freeing Up Resources: Sometimes, after you close a program, it still is running on the background. It can slow down your system. In this case, you should free up system resources (like memory) using the kill command Linux for closing the program. 

Note: You should use the kill process Linux with caution. If you don’t, you may stop essential processes in your system which can lead to an unstable or crashed system. Just use this command when necessary. 

How to Find a Program's ID or Name

To stop a program using the kill command, you need to know the program’s name or ID first. You can find it by searching the process’s name (or part of the name) or its ID number (PID). To find a process in Linux, there are different ways: 

  1. Using the ps command
  2. Using the pgrep or pidof command
  3. Using the top command

Now, we explain each way here:

1- Using the ps Command

Using this command, all the running processes will be listed. You can adjust the details it shows with different options. The basic form of this command is:

ps [options]

The common options include:

  • -a: This option shows processes for all users, instead of the current user.
  • -u: This option is good if you want more details about each process.
  • -x: This option includes processes not controlled by users (such as system daemons).

You can combine the options if you want all of the mentioned information. For example, if you run ps -aux command, a detailed list of all processes running on the system will be shown, just like the following picture:

Linux ps Command

2- Using the pgrep or pidof command

If you want to find specific processes in Linux, the pgrep command is the best way. Using this, is like a keyword or search term as it searches for processes based on a pattern. This pattern can include wildcards, such as a*, where * stands for any characters that follow. 

pgrep

Here you can see the format for the pgrep command:

pgrep [options] [pattern]

Common options for pgrep include:

  • -l: Shows the names and IDs (PIDs).
  • -n: Shows just the most recent process.
  • -o: Shows just the oldest process.
  • -u: Shows the processes that are belong to a specific user.
  • -x: Match processes exactly with the given pattern.

For instance, if you run this command: pgrep -l -u root, all the names, and PIDs of all processes owned by the root user will be displayed. Just like the following picture shows:

Linux pgrep command

Pidof

If you know the name of the process, the pidof command is useful to find the ID (PID) of that process. The format for this command is like this:

pidof [options] [program]

The options for this command include:

  • -c: Only show PIDs from a single root directory.
  • -o: Exclude certain PIDs (list the PIDs to exclude after this option).
  • -s: Show only one PID.
  • -x: Also show PIDs of shells running scripts.

For instance, imagine a process named `snapd`. To find the PID for that, run pidof snapd and you see the following results:

pidof snapd

3- Using the top command

Do you want to see an overview of all the running processes in your Linux system? So, the top command is the simplest way that you can use. All you need to do is run this command:

Top

As a result, you can see information like process IDs, users, memory usage, CPU usage, and how long each process has been running. Just like the following photo shows as an example:

When you want to exit this interface, press q. 

You need to have the proper permissions for killing a process. So before doing that, check if you have the needed permissions. The root users can stop any program that they want. You can either do this by using the sudo command before the kill command. If you want, you can switch to root user using the su command to stop any program that you want. For killing processes, you send your system signals. Different types of signals include:

  • SIGKILL: This is the fastest and most reliable way to end a process and used for processes which are causing errors and need to be stopped by force immediately. If this signal didn’t work, your operating system may have a problem. 
  • SIGTERM: This signal for closing the program can be blocked or managed by the process. This is not as forceful as SIGKILL and is a softer way of stopping programs. 

Now, let’s explore the methods for killing a process. 

killall Command

You can stop processes by name using the killall command. This command sends a SIGTERM signal by default. Using this command, it is possible to stop multiple processes at once. The normal format of this command is like this:

killall [process]

The options include:

  • -e: Match the process name exactly.
  • -I: Ignore case when finding the process name.
  • -i: Ask for confirmation before stopping the process.
  • -u: Only stop processes owned by a specific user.
  • -v: Show if the process was successfully stopped.

As we mentioned, using killall command with -i (killall -i) will ask for confirmation before stopping the process. You can see the example here:

Linux killall Command

How to kill processes older than TIME

Sometimes you need to stop processes based on the period that they’ve been running. To do this, you can use the following options which are part of the Linux kill process:

  • -o: Stops all processes that have been running longer than a set time (e.g., 15m for 15 minutes).
  • -y: Stops all processes that have been running for less than a set time.

For example, if you want to stop all processes older than 15 minutes, you can use this command:

killall -o 15m

How to kill processes younger than TIME

And to stop processes that have been active for less than 15 minutes, use this command:

killall -y 15m

pkill Command

This command functions like the pgrep. Unlike the pgrep which just could find a process, this one can stop the processes based on factors like their names. This command sends SIGTERM signal, so it is not a good option if you need to close a program by force. This is the basic format of pkill command:

pkill [options] [pattern]

Here are the options for this command:

  • -n: Only stop the newest process found.
  • -o: Only stop the oldest process found.
  • -u: Only stop processes owned by a specific user.
  • -x: Only stop processes that exactly match the pattern.
  • -signal: Send a different signal to the process instead of the default SIGTERM.

For instance, imagine you want to kill the newest process created by the user “bosko”. In this case, you’ll use the following command:

pkill -n -u bosko

kill -9 Command

Do you want to stop a process that isn't responding? So, the kill -9 is the best option here. This is the basic form of the command:

kill -9 [processID]

Or:

kill -SIGKILL [processID]

The kill -9 command stops the process immediately as it sends a SIGKILL. Unlike the regular kill command Linux which sends a SIGTERM signal and can be ignored by the unresponsive program, kill 9 is the force option and it can always stop the program. 

xkill command

To close a server's connection to clients, you need to use xkill command. This is the format for the xkill command:

xkill [resource]

In the situation that a server has unwanted processes running, an xkill command Linux can stop them. 

In the resource section, you need to replace a resource. If not, a window will be shown and you can close any resource by clicking on their window. This is an example:

Linux xkill command

top Command

Do you want to see and manage the processes currently running on your system? So, in this case, you can use the top command too. All you need to do is run this simple command:

Top

In the top interface, you can stop a specific process. In this case, you should know the PID of the process you want to close. Just press “k” to do that.

Linux top Command

The kill command is built-in and available with various shells. The syntax of the command may vary among different shells. To check the kill command’s location on your shell, you can hit the "type" command, as shown below.

type -a kill

Linux Kill Process

Kill Command in Linux with examples

Syntax-

kill [OPTIONS] [PID]...

Where-

  • signalNumber: a non-negative decimal integer.
  • signalName: it is a symbolic signal name.
  • PID: displays the entire list of processes.

To get the list of all the available signals, you can hit the kill command along with the “-l” option, as shown below.

kill -l

Kill Command in Linux

You can specify the signals in three different ways as per your requirement-

  • number (e.g., -1 or -s 1).
  • Along with the “SIG” prefix (e.g., SIGHUP).
  • Without the “SIG” prefix (e.g., HUP).

The following commands can be interchangeably used with one another, as they all perform the same action.

kill -1 PID_NUMBER

kill -SIGHUP PID_NUMBER

kill -HUP PID_NUMBER

  • PID > 0, this command will send a signal to process with the specified PID.
  • PID=0, this command will signal all the processes present within the current process group, i.e., the same GID. 
  • PID = -1, this command will send the signal to all the processes having the same UID as the user who invokes the kill command. If the specific user is the root user, then the signal will be sent to all processes except the init and the kill process itself.
  • PID < -1, this command will send the signal to all the processes available in the process group eq with the GID equal to the PID’s absolute value.

If you are a regular user other than root, you are authorized to send the signal only to their processes, not even to the processes of another user. 

Different values for signal number and signal name

Signal Number

Signal Name (short name)

0

SIGNULL (NULL)

1

SIGHUP (HUP)

2

SIGINT (INT)

3

SIGQUIT (QUIT)

9

SIGKILL (KILL)

15

SIGTERM (TERM)

24

SIGSTOP (STOP)

25

SIGTSTP (STP)

26

SIGCONT (CONT)

To terminate a process, you can simply use the kill command. But, you need to specify the process ID number (PID) to identify the specific process among all, as each process has a different PID. Follow the commands below listed to accomplish your job.

ps 

Linux ps 

pid 

Linux pid 

Now, you have the PID of the “settime” process as “55” which you can specify with the kill command to kill the specific process, as shown below.

kill -9 55

This command will kill the “settime” process. 

You can also use the kill command to send the required HUP signal, which will help reload the processes' settings. 

Another way of using the kill command is to send the HUP signal, which tells the process to reload its settings. In the below example, we have reloaded the process with process id-38 (bioset), as shown below.

Linux Sudo

Make sure to run the above command with the Sudo privileges.

There’s also a SIGSTOP signal for pausing the processes, instead of closing them. This is like a pause button. Imagine you want to send a SIGSTOP to a process named “xed” with the PID of “30866”. In this case, you can use this command:

kill -s SIGSTOP 30866

After that, to check the status of the process, use this command:

ps 30866

If you notice a “T” in the output in the “STAT” column, you can ensure the process is paused. 

This method is useful when you want to pause a program temporary. 

How to Kill Only Processes Running as a Specific User

Let’s explain this method with an example. Imagine you want to stop a process named “vim” that is running under the user named “vivek”. In this situation, you can run this command:

killall -u vivek -9 vim

As a result, all the “vim” processes belonging to the “vivek” user will be forcefully killed. 

Whenever a process or application starts malfunctioning or becomes unresponsive to your actions, then you have the leverage to terminate that process manually or forcefully. Linux offers a simple command kill to do that. We have explained how this command works and various options to kill the process in the Linux operating system.

People are also reading:

If you have root access, you can kill all the processes including your own and those of other users. But if you donā€™t have root access, you can kill processes that you have permission to kill them, including your own ones.

When you run the ā€œkillā€ command, you target a specific process using its PID. But if you use ā€œkillallā€ command, all processes with a given name will be stopped.

There are different commands that you can use to find the PID of a process including ā€œpsā€, ā€œpgrepā€, and ā€œtopā€ command. These commands list running processes and their IDs.

Susith Nonis

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.