If a Linux process stops responding or consumes too many resources, you may need to delete it
Most processes have their own closing methods. Unfortunately, processes can malfunction and not allow them to shut down. If a running background process is not responding, you need to use a command to remove it.
Here is a complete guide on how to kill a Linux process using the command line.
What processes can you kill in Linux?
Before you delete or terminate a process, you must consider permissions.
A root user can kill all processes. You can add sudo before a command to run it as root or get a root shell with su. Then run the command.
Killing a process
sends a termination message to the given process. There are several types of termination messages including:
- – SIGKILL is the best way to kill a process. It will always kill a process and kill the process abruptly, generating a fatal error. SIGKILL should always work. If it doesn’t work, the operating system has crashed.
- – SIGTERM tries to kill a process, but unlike SIGKILL it can be blocked or handled in another way. It can be considered a gentler way of trying to finish a process.
For most purposes, SIGKILL will be the fastest and most effective method to finish the process.
Step 1: View running Linux processes
The top command
is the easiest way to get a complete overview of the
processes that are currently running. To
view a list of all processes that are currently running, use the command:
The top command will reveal the process IDs and users, plus how much memory and CPU power each process uses
To remove processes directly from the top interface, press k and enter the process ID.
To exit the top interface, Press Q.
Step 2: Locate the process to
kill Before you
can kill a process, you need to find it. There are several ways to search for a process in Linux. Processes can be located by a process name (or a partial process name) or a process ID (also known as “pid”).
Locate a process with
the ps command
The ps command displays information similar to the top, although it will not be in the form of an interface. Instead, the ps command provides a complete list of running processes, formatted based on the tags you add.
The most common options to add to this are “-aux”:
- -a. View processes for all users instead of just the current user.
- -u. Provide detailed information about each of the
- x-processes. Include processes controlled not by users but by daemons.
For example, the ps -aux command will return a detailed list of all processes.
Find the PID with pgrep or pidof
The Linux pgrep command is a more complex way to find a process. This command will return processes based on specific selection criteria, known as a pattern. The pattern is a regular expression, such as a*, where * would be a wildcard.
pgrep <options> <pattern>
These are the options that can be used with this command:
- -l. List both process names and PIDs.
- -n. Return the most recent process.
- -or. Return the oldest process.
- -u. Only find processes that belong to a specific user.
- -x. Only find processes that exactly match the given pattern.
The pgrep -u root command displays all root-owned processes. The pgrep -u root ‘a*’ command returns root-owned processes that begin with the letter “a”.
The pidof command is used to find the ID of a process, as long as you know the name of the process.
pidof <options> <program>
Some options can be included, such as:
- -c. Only return PID within a single root directory.
- -or. Skip certain PIDs (include processes that should be skipped after the indicator).
- -s. Only return a single PID.
- -x. It also returns PIDs of shells that run scripts.
Step 3: Use
the kill command options to terminate a process
There are a few different methods to kill a process on Linux, depending on whether you know the name of the running process, the pid of the process, or simply how long the process has been running
The killall command is used to kill processes by name. By default, it will send a SIGTERM signal. The killall command can kill multiple processes with a single command.
Several options can be used with the killall command:
- -e. Find an exact match for the process name.
- -I. Ignore case letters when trying to find the process name.
- -i. Ask for additional confirmation when killing the process.
- -u. Only delete processes owned by a specific user.
- -V. Report whether the process has been successfully eliminated.
In addition to killing processes based on the name, the killall command can also be used to kill based on the age of the process, using the following commands:
- -o. Use this flag with a duration to remove all processes that have been running more than that amount of time.
- -and. Use this flag with a duration to remove all processes that have been running less than that amount of time.
The killall -o 15m command will kill all processes that are longer than 15 minutes,
while the killall -y command will kill all processes that last less than 15 minutes
The pkill command is similar to the pgrep command, in that it will kill a process based on the process name. in addition to other qualifying factors. By default, pkill will send the SIGTERM signal.
The pkill options <options> <pattern>
- -n. It only kills the newest of the processes that are discovered.
- -or. It only kills the oldest of the processes that are discovered.
- -u. Only delete processes owned by the selected user.
- -x. It only kills processes that exactly match the pattern.
- -signal. Send a specific signal to the process, instead of SIGTERM.
If you know a process ID, you can kill it with the command:
The kill command will kill only one process at a time with the given process ID. It will send a SIGTERM signal indicating a process to stop. Wait for the program to run its shutdown routine.
The –signal command can be used to specify a signal other than SIGTERM.
kill -9 is a useful command when you need to shut down an unresponsive service. Run it similarly to a normal
kill command: kill -9 <processID>
kill -SIGKILL <processID>
The kill -9 command sends a SIGKILL signal that tells a service to shut down immediately. An unresponsive program will ignore a kill command, but will close each time a kill -9 command is issued. Use this command with caution. It skips the standard shutdown routine, so unsaved data will be lost.
Your operating system does not work properly if a SIGKILL token does not shut down a service.
The top command provides an interface through which a user can navigate through processes that are currently running
To kill a specific process, insert k from the top interface, and then enter the desired process ID
. xkill command The xkill command
is a special type of command that closes a particular server’s connection to clients. xkill
server has opened a number of unwanted processes, xkill will abort these processes. If xkill
is run without specifying a resource, an interface will open that allows the user to select a window to close.
Key points about terminating
a Linux process
- When a process cannot be closed otherwise, It can be killed manually via the command line.
- To kill a process on Linux, you must first find the process. You can use the top, ps, pidof or pgrep commands.
- Once you have found the process you want to kill, you can kill it with the killall, pkill, kill, xkill or top commands.
- When killing a process, you can send a SIGHUP, SIGKILL, or SIGTERM termination signal.
- You must have permission to kill a process, which you can obtain by using the sudo command.
In this article, we cover several ways to kill processes in Linux. It is critical to learn and understand these Linux termination commands for system management and administration.