If a Linux process becomes unresponsive or consumes too many resources, you may need to kill it. Most processes have their own methods of shutting down. Unfortunately, processes can malfunction and require running a command to manually kill it.
This guide will show you how to kill a Linux process using the command line.
- A system running Linux.
- A user account with root privileges.
- Access to the terminal.
How to Find Process ID or Process Name
Before killing a process, you need to locate it. Processes can be found by the process name (or a partial process name) or the process ID (also known as a PID).
There are multiple ways to find a process in Linux:
- Via the
- Via the
- Via the
The sections below show different ways to search for a process in Linux.
Locate a Process with ps Command
ps command provides a complete listing of running processes, formatted based on the tags you add. The syntax for the
ps command is:
The most common options to add to
-a. View processes of all users rather than just the current user.
-u. Provide detailed information about each of the processes.
-x. Include processes that are controlled not by users but by daemons.
For example, the command
ps -aux returns a detailed list of all processes:
Find PID with pgrep or pidof
pgrep Linux command is a more complex way of finding a process. The
pgrep command returns processes based on specific selection criteria, known as the pattern. The pattern is a regular expression, such as
* is a wildcard.
pgrep [options] [pattern]
The options that can be used with
-l. List the process names and the PIDs.
-n. Return the newest process.
-o. Return the oldest process.
-u. Only find processes that belong to a specific user.
-x. Only find processes that exactly match the given pattern.
For example, the command
pgrep -l -u root displays the names and PIDs of all processes owned by root:
pidof command is used to find the ID of a process, provided that you know the process name.
pidof [options] [program]
Some of the options that
pidof accepts are:
-c. Only return PIDs within a single root directory.
-o. Omit certain PIDs (include the processes to omit after the flag).
-s. Only return a single PID.
-x. Also returns PIDs of shells that are running scripts.
For example, to get the PID of the
snapd process, run
View Running Processes with top
The top command is the easiest way to get a complete overview of the currently running processes. To view a list of all active processes, run the command:
top command interactive mode starts, shows the process IDs, the users, the amount of memory and CPU power each process uses, the running time, etc.
To exit the
top interface, press q.
How to Kill a Process
Make sure to consider permissions before killing or terminating a process. A root user can kill all processes. You can either add
sudo before a command to run it as root, or obtain a root shell with
su. Then, execute the termination command.
Killing a process sends a termination message to the given process. There are multiple types of termination messages, including:
SIGKILL- The ultimate way of killing a process. It always kills a process abruptly, generating a fatal error.
SIGKILLshould always work. If it doesn't, the operating system has failed.
SIGTERM- It attempts to kill a process, but, unlike
SIGKILL, it may be blocked or otherwise handled. It is a gentler way of terminating a process.
For most purposes,
SIGKILL is the fastest and most effective method to terminate the process.
The sections below list the most commonly used commands for killing a process and provide the steps for using each command.
killall command kills processes by their name. By default, it sends a
SIGTERM signal. The
killall command can kill multiple processes with a single command. The syntax is:
killall command accepts several options:
-e. Find an exact match for the process name.
-I. Ignore the case when trying to find the process name.
-i. Ask for additional confirmation when killing the process.
-u. Only kill processes owned by a specific user.
-v. Report back on whether the process has been successfully killed.
The following example shows the use of the
-i option, instructing
killall to ask for confirmation before terminating the process:
In addition to killing processes based on name, the
killall command can also be used to kill based on the age of the process. Use the following commands:
-o. Use this flag with a duration to kill all processes that have been running for more than that amount of time.
-y. Use this flag with a duration to kill all processes that have been running for less than that amount of time.
killall -o 15m command kills all processes that are older than 15 minutes, while the
killall -y 15m command kills all processes that have been active for less than 15 minutes.
pkill command is similar to the
pgrep command, in that it kills a process based on the process name, in addition to other qualifying factors. By default,
pkill sends the
SIGTERM signal. The syntax is:
pkill [options] [pattern]
pkill options include:
-n. Only kill the newest of the processes that are discovered.
-o. Only kill the oldest of the processes that are discovered.
-u. Only kill the processes owned by the specified user.
-x. Only kill the processes that match the pattern exactly.
-signal. Send a specific signal to the process, rather than
The following example demonstrates how to kill the newest process created by the user
pkill -n -u bosko
kill command terminates processes via the process ID. The syntax is:
kill [process ID]
kill command kills a single process at a time with the given process ID. It sends a
SIGTERM signal instructing a process to stop. It waits for the program to run its shutdown routine.
-signal option can be used to specify a signal other than
kill -9 Linux Command
kill -9 is a useful command when you must shut down an unresponsive service. Run it similarly to the regular
kill -9 [processID]
kill -SIGKILL [processID]
kill -9 command sends a
SIGKILL signal to a service, shutting it down immediately. An unresponsive program ignores a
kill command, but it shuts down whenever a
kill -9 command is issued. Use this command with caution since it bypasses the standard shutdown routine, and any unsaved data will be lost.
xkill command is a special command that closes a given server's connection to clients. The syntax of the
xkill command is:
If a server has opened some unwanted processes,
xkill aborts these processes.
xkill is run without specifying a resource, then an interface opens up to let the user select a window to close. For example:
top command provides an interface through which a user can navigate the currently running processes. To start the interface, run the command:
To kill a specific process, press
k when in the interface, and then enter the PID of the process you want to terminate:
Note: Learn how to use the nohup command to block the
SIGHUP signal and allow processes to complete even after logging out from the terminal/shell.
In this article, we covered several ways to kill processes in Linux. It is critical to learn and understand these Linux termination commands for system management and administration.