In certain situations within Linux, processes can hinder users from initiating new tasks or installing software. For example, invoking the `apt` command may result in an error message similar to “could not get lock /var/lib/dpkg/lock-frontend.
” This issue often arises because the `apt` command is already in use by another process, and terminating that conflicting process can resolve the problem. Terminating a process, often referred to as “killing” a process, effectively forces it to quit, enabling users to resolve system issues without resorting to a full system reboot.
This tutorial will first provide a concise overview of Linux processes before examining the proper methods for terminating processes in Linux.
Understanding Linux Processes
Whenever you execute an application like Chrome, open a terminal window, or schedule a cron job on your Linux system, you are initiating processes. Linux processes are broadly categorized into two types:
Background Processes
Background processes are designed to operate autonomously, starting automatically without requiring direct user interaction or intervention. These processes typically run silently in the background, handling system tasks without user visibility.
Foreground Processes
Foreground processes, in contrast, are launched directly by users and are interactive in nature. These processes actively engage with the user, often through a terminal or graphical interface, and their execution is typically visible and directly controlled by the user.
Upon initiation, every process, such as launching Chrome, is assigned a unique Process ID (PID). This PID is a distinctive five-digit number that allows the system to track and manage individual processes.
During Linux system startup, the very first process to launch is the `init` process. Consequently, it typically holds the PID of 1. The `init` process serves as the master process, from which all other processes are eventually derived. Unlike regular processes, the `init` process is essential for system operation and cannot be terminated using standard process-killing commands.
For example, to identify processes associated with the Apache2 web server, execute the following command:
ps aux | grep apache2
In the output, the first numerical value on each line represents the PID of an Apache2 process. To extract only the PIDs, you can use the `pidof` command:
pidof apache2
As you can see, the PIDs listed by `pidof` match those displayed in the `ps aux` output, confirming they are indeed the Process IDs for Apache2 server instances.
Read: Monitoring system processes in Ubuntu using htop
Another useful command, `ps -A`, also displays PIDs alongside process names, providing a clear overview of running processes and their identifiers:
Terminating Processes with the `kill` Command
Occasionally, a process may become unresponsive, stall, or interfere with other system operations. In such cases, Linux offers mechanisms to terminate these problematic processes. The `kill` command (along with `killall` and `pkill`, discussed later) is a primary tool for terminating processes, requiring the Process ID (PID) of the process you intend to terminate.
The basic syntax of the `kill` command is:
kill [signal or option] PID(s)
[kill process Linux]
Process termination permissions in Linux are straightforward:
– Users can terminate processes they own.
– Terminating system-level processes or processes owned by other users requires root (superuser) privileges.
The `kill` command operates by sending specific signals to a process. If no signal is explicitly specified, `kill` sends the `TERM` signal by default, which requests the process to terminate gracefully. To explore the complete list of Linux signals and their descriptions, consult the `signal` manual page by running:
man signal
Alternatively, the command `kill -l` provides a concise list of available signal names:
kill -l
kill process ubuntu command – listing signals
Read: How to fix high memory usage in Linux
Frequently used signals include `INT`, `HUP`, `STOP`, `KILL`, `CONT`, and `0`.
Signals can be specified in three ways with the `kill` command:
- By number (e.g., `-15`).
- With the “SIG” prefix (e.g., `-SIGKILL`).
- Without the “SIG” prefix (e.g., `-KILL`).
Common signal values for the `kill` command are:
linux kill command signals – common options
For most situations, the `SIGTERM` signal (default) is the safest and most appropriate option for terminating processes gracefully.
To demonstrate process termination using `kill`, we first identify the PID of the target process, for example, using `ps -A`. Once you have the PID, use the `kill -9` command to forcefully terminate the process:
kill -9 PID
Let’s terminate the Firefox process. In the example screenshot, an active Firefox browser is running (indicated by red dots on the Firefox icon):
The PID of the Firefox process is identified as 18447. To terminate it, execute the command `kill -9 18447`:
kill -9 18447
[Linux kill process example]
kill linux process – Firefox termination
After executing the command, the red dots beside the Firefox icon disappear, indicating process termination. To verify process termination, you can use commands like:
ps -A | grep firefox
ps aux | grep firefox
pidof firefox
pgrep firefox
For example, running `ps -A | grep firefox` confirms that no Firefox processes are found:
Using the signal name (SIGKILL) with the `kill` command achieves the same result:
kill -SIGKILL 18447
The `kill` command can also terminate multiple processes simultaneously by specifying multiple PIDs:
kill pid1 pid2 pid3
Read: How to list, start and stop services at boot time in Linux Ubuntu/Debian
Terminating Processes by Name with `pkill`
The `pkill` command offers a more convenient way to terminate processes by specifying their name instead of their PID. This eliminates the need to first look up the PID. However, caution is advised: mistyping the process name with `pkill` could unintentionally terminate unintended processes. To mitigate this risk, you can first use `pgrep` to verify the PIDs associated with a given process name:
pgrep firefox
This command returns the PIDs of processes matching the name “firefox.” After verifying the PIDs, you can safely use `pkill` to terminate those processes by name:
pkill firefox
As shown above, the `pkill` command successfully terminates the Firefox process, indicated by the disappearance of the red dots next to the browser icon.
Read: The architecture of Linux operating system
Terminating Multiple Processes with `killall`
Warning: Be particularly cautious when using `killall` on Solaris systems, as it may terminate all processes belonging to the current user. Exercise caution to avoid unintended data loss or system instability.
For scenarios involving processes with multiple instances or numerous child processes, the `killall` command is a suitable option. `killall` functions similarly to `pkill` but requires the exact process name as an argument, rather than a search term. Its syntax is:
killall [signal or option] Exact_Process_Name
For example, to terminate all instances of “Web Content” (Firefox browser tabs) and their child processes, identify the exact process name using a tool like `ps` or `top`:
Then, execute the `killall` command, enclosing the process name in quotes if it contains spaces:
killall -9 ‘Web Content’
killall command in linux – terminating Firefox tabs
As shown in the screenshot, the `killall` command effectively terminates all Firefox tabs (Web Content processes).
The `-y` (younger than) option with `killall` allows you to target processes based on their start time, terminating processes that have been running for less than a specified duration. Time periods are indicated by numbers followed by units:
`s` (seconds), `m` (minutes), `h` (hours), `d` (days), `w` (weeks), `M` (months – uppercase “M”), and `y` (years).
For example, to terminate a process named “FOX” that started within the last 5 minutes, while leaving older “FOX” processes running, use:
killall -y 5m FOX
[how to kill running process in Linux by age]
Conversely, the `-o` (older than) option targets processes that have been running longer than a specified period. To terminate all `httpd` (Apache) processes older than 2 days, use:
killall -o 2d httpd
For comprehensive information on `killall` and its options, consult the manual page:
man killall
Conclusion: Effectively Managing Linux Processes
The commands detailed in this article empower users to identify and terminate unwanted, misbehaving, or malfunctioning Linux processes safely and efficiently.
However, always exercise caution when terminating processes. First, accurately identify the target process to avoid unintended terminations. Second, when using `killall` or `pkill`, carefully verify the process name to ensure you are targeting the correct application and not inadvertently terminating critical system processes or other user applications.
If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.