Introduction
If a Linux process becomes unresponsive or consumes excessive resources, you may need to terminate it. While most processes have their own methods for shutting down, they can sometimes malfunction, necessitating the use of a command to manually kill them.
This guide will demonstrate how to terminate a Linux process using the command line.
Prerequisites
- A system running Linux.
- A user account with root privileges.
- Access to the terminal.
How to Find Process ID or Process Name
Before terminating a process, you need to locate it. You can find processes by their name (or a partial name) or by their process ID (PID).
There are several methods to find a process in Linux:
- Using the
ps
command. - Using the
pgrep
orpidof
commands. - Using the
top
command.
The following sections will detail various ways to search for a process in Linux.
Locate a Process with ps Command
The ps
command offers a detailed list of running processes, formatted according to the options you specify. The syntax for the ps
command is:
ps [options]
The most common options to use with ps
are:
-a
: View processes of all users, not just the current user.-u
: Provide detailed information about each process.-x
: Include processes not controlled by users but by daemons.
For example, the command ps -aux
returns a detailed list of all processes.
Find PID with pgrep or pidof
The pgrep
command in Linux offers a more sophisticated method for finding processes. It returns processes based on specific selection criteria, known as a pattern. The pattern is a regular expression, such as a*
, where *
is a wildcard.
pgrep [options] [pattern]
The options available for use with pgrep
include:
-l
: List the process names and PIDs.-n
: Return the newest process.-o
: Return the oldest process.-u
: Only find processes belonging 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 processes belonging to the user root
.and PIDs of all processes owned by root:
The pidof
command is used to find the ID of a process when 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 (specify the processes to omit after the flag).-s
: Only return a single PID.-x
: Also return PIDs of shells running scripts.
For example, to get the PID of the snapd
process, run pidof snapd
.
View Running Processes with top
The top
command provides the most straightforward way to obtain a comprehensive overview of currently running processes. To see a list of all active processes, simply run the command:
top
When you initiate the interactive mode of the top
command, it displays various details including process IDs, users, memory and CPU usage of each process, running time, and more.
To exit the top
interface, press q.
How to Kill a Process
Before terminating a process, it’s important to consider permissions. A root user can terminate all processes. You can either prepend sudo
before a command to execute it as root, or acquire a root shell with su
, and then execute the termination command.
Terminating a process sends a termination message to the specified process. There are various types of termination messages, including:
SIGKILL
: This is the most forceful way to terminate a process. It abruptly ends the process, causing a fatal error.SIGKILL
should reliably terminate a process. If it fails to do so, it indicates a failure in the operating system.SIGTERM
: UnlikeSIGKILL
,SIGTERM
attempts to terminate a process but may be blocked or handled differently. It’s a gentler approach to terminating a process.
For most purposes, SIGKILL
is the quickest and most effective method to terminate a process.
The following sections outline the commonly used commands for terminating a process and provide step-by-step instructions for utilizing each command.
killall Command
The killall
command terminates processes based on their names. By default, it sends a SIGTERM
signal. killall
can terminate multiple processes with a single command. The syntax is:
killall [process]
The killall
command offers several options:
-e
: Match the process name exactly.-I
: Ignore case sensitivity when searching for the process name.-i
: Request additional confirmation before terminating the process.-u
: Terminate only processes owned by a specific user.-v
: Provide feedback on whether the process was successfully terminated.
The following example demonstrates the use of the -i
option, prompting killall
to request confirmation before terminating the process:
In addition to terminating processes by name, the killall
command can also terminate processes based on their age. You can use the following commands:
-o
: Specify a duration to terminate processes that have been running for longer than that period.-y
: Specify a duration to terminate processes that have been running for less than that period.
For instance, the command killall -o 15m
terminates all processes older than 15 minutes, while killall -y 15m
terminates processes active for less than 15 minutes.
pkill Command
The pkill
command operates similarly to the pgrep
command, as it terminates processes based on their names, along with other qualifying criteria. By default, pkill
sends the SIGTERM
signal. The syntax is:
pkill [options] [pattern]
Options for the pkill
command include:
-n
: Terminate only the newest discovered processes.-o
: Terminate only the oldest discovered processes.-u
: Terminate processes owned by the specified user.-x
: Terminate processes that exactly match the pattern.-signal
: Send a specific signal to the process instead ofSIGTERM
.
Here’s an example illustrating how to terminate the newest process created by the user bosko
:
pkill -n -u bosko
kill Command
The kill
command terminates processes via the process ID. The syntax is:
kill [process ID]
The kill
command terminates a single process at a time, using the provided process ID. It sends a SIGTERM
signal, directing the process to stop and allowing it to execute its shutdown routine.
The -signal
option allows specifying a signal other than SIGTERM
.
kill -9 Linux Command
kill -9
is a helpful command for forcefully shutting down an unresponsive service. You can execute it similar to the standard kill
command:
kill -9 [processID]
Or:
kill -SIGKILL [processID]
The kill -9
command sends a SIGKILL
signal to a service, forcing it to shut down immediately. Unlike a regular kill
command, an unresponsive program cannot ignore a kill -9
command. However, it’s important to use this command cautiously as it bypasses the standard shutdown routine, potentially leading to the loss of unsaved data.
xkill command
The xkill
command is a specialized tool used to terminate connections between a server and its clients. The syntax for the xkill
command is:
xkill [resource]
When unwanted processes are opened by a server, xkill
terminates these processes.
If xkill
is executed without specifying a resource, it opens an interface allowing the user to select a window to close. For example:
top Command
To initiate an interface displaying the currently running processes, use the top
command. Run it like so:
top
To terminate a specific process, press “k” while in the interface, then input the PID of the process you wish to kill:
Note: Discover how to utilize the nohup
command to shield processes from the SIGHUP
signal, enabling them to continue executing even after logging out from the terminal/shell.
Conclusion
In this article, we’ve explored various methods for terminating processes in Linux. Mastering these Linux termination commands is essential for effective system management and administration.