Mastering the 'pkill' Command (with examples)
pkill
is a versatile command-line utility widely used in Unix-based systems to send signals to processes based on their names, rather than process IDs. This ability makes pkill
particularly useful for terminating or otherwise manipulating processes without needing to identify them precisely by their numeric identifiers. It leverages the powerful signaling mechanism in Unix systems, which allows you to take a variety of actions on processes, from instructing a graceful shutdown to forcing an abrupt termination.
Kill all processes which match
Code:
pkill "process_name"
Motivation for using this example:
Imagine you’re a system administrator or a software developer managing a server where a particular application, such as a custom logging application, is running amok due to a bug. This application could be consuming excessive resources, slowing down or even halting system performance. In such a situation, you need a rapid remedy. Using pkill "process_name"
, you can quickly terminate all instances of this problematic application to restore system stability without needing to hunt down individual process IDs.
Explanation for every argument given in the command:
"process_name"
: This is the name of the process or application you wish to target.pkill
will scan all running processes and terminate those with names matching this string. This approach is direct and effective when you know the exact name of the application causing trouble.
Example output:
While the command itself does not typically produce output unless there’s an error, executing this successfully will result in the targeted processes ceasing operation. On checking the process table, you’ll notice the absence of any instances of “process_name”.
Kill all processes which match their full command instead of just the process name
Code:
pkill -f "command_name"
Motivation for using this example:
Sometimes, multiple instances might share the same process name, but have different command-line arguments starting them. This can occur with scripts or applications that run with different configurations specified at startup. Say you have a Python script running with various settings based on different command-line arguments. Using pkill -f
, you can target these processes more precisely by specifying a unique part of their command-line invocation instead of just the process name.
Explanation for every argument given in the command:
-f
: This flag instructspkill
to match against the full command line instead of just the process name, giving you fine-grained control over which processes should be signaled."command_name"
: Here, this refers to the full command or a distinctive part thereof. By matching against the entire command line, you can terminate only those processes started with specific arguments.
Example output:
As with the basic pkill
, this command does not return direct output but will effectively end processes matching the full command criteria. Verification through system monitoring tools will show that only the specified processes no longer exist.
Force kill matching processes (can’t be blocked)
Code:
pkill -9 "process_name"
Motivation for using this example:
There are cases where processes become unresponsive or hung and do not shut down gracefully when a regular termination signal is sent. Such situations often arise when a process is stuck due to deadlocks or misbehaving network connections. pkill -9
sends the SIGKILL signal, which the designated process can’t ignore, ensuring that it is forcibly terminated.
Explanation for every argument given in the command:
-9
: The-9
option sends the SIGKILL signal. This is a brute force approach, interrupting and terminating the process immediately, bypassing any cleanup it might do on receipt of a regular termination signal."process_name"
: This targets the specific processes to be forcefully terminated. The use of quotation marks ensures the name is treated as a single entity despite containing spaces or special characters.
Example output:
Executing this command will eliminate the process immediately, often noticeable by the sudden reduction in system resource usage or the disappearance of the process from utilities like top
or ps
.
Send SIGUSR1 signal to processes which match
Code:
pkill -USR1 "process_name"
Motivation for using this example:
Application processes can often be customized to respond to user-defined signals. By sending a SIGUSR1 signal, you can perform user-specific actions without stopping a process. This is particularly useful for processes that can refresh, reconfigure, or log specific debug information upon receiving this signal. For example, web servers or monitoring applications might be instructed to perform certain tasks upon receiving SIGUSR1.
Explanation for every argument given in the command:
-USR1
: This specifies that the SIGUSR1 (user-defined signal 1) is sent, signaling processes to perform a user-defined action if configured to do so."process_name"
: This allows you to select the processes which should receive this signal.
Example output:
The signal’s response is dependent on the process’s pre-programmed behavior. Though pkill
itself does not directly return output, you might see actions like log entries or configuration reload messages, depending on the signal’s programmed effect.
Kill the main firefox
process to close the browser
Code:
pkill --oldest "firefox"
Motivation for using this example:
In many situations, multiple processes might bear the same name but serve different roles. For instance, when firefox
runs, it may spawn several child processes for tabs, plugins, or extensions. If you wish to close the browser entirely, rather than just a tab or specific functionality, terminating the main parent process (the oldest one) is required. pkill --oldest
efficiently does this by targeting the longest-running instance of firefox
.
Explanation for every argument given in the command:
--oldest
: This option is used to target the oldest running process with the specified name, which often serves as the parent or main process in multi-process applications."firefox"
: The process name specifies that you are focusing onfirefox
, a commonly used web browser known for employing multiple processes.
Example output:
Upon executing this command, firefox
will close entirely. Users will see all browser windows and tabs shut down, which can be confirmed by its absence in their graphical user interfaces or system process lists.
Conclusion
pkill
provides a simple yet powerful interface for process management and manipulation in Unix-like systems. By comprehensively understanding and employing its different options and signals, users can effectively control the processes running on their systems, tailoring responses to specific needs whether it be graceful shutdowns, forced terminations, or process-specific action signaling.