2024-02-20
pkill
’s Functionalitypkill
sends signals to processes matching specified criteria. By default, it sends the SIGTERM
signal (termination signal), giving processes a chance to gracefully shut down. If a process ignores SIGTERM
, pkill
can be instructed to use a more forceful signal like SIGKILL
(kill signal), which immediately terminates the process without cleanup.
The simplest use case involves killing processes based on their names. Let’s say you have multiple instances of the firefox
browser running:
pkill firefox
This command will send a SIGTERM
signal to all processes whose names contain “firefox”. This includes processes like firefox
, firefox-bin
, or potentially other related processes.
As mentioned, pkill
defaults to SIGTERM
. To use a different signal, use the -SIG
option followed by the signal name:
pkill -SIGKILL firefox
This command will send SIGKILL
to all “firefox” processes, forcing immediate termination. Be cautious with SIGKILL
, as it prevents graceful shutdown and might lead to data loss in some cases.
For more flexible matching, pkill
supports regular expressions using the -f
option. This allows for complex pattern matching beyond simple substring matching.
For example, to kill all processes whose names start with “chrome”:
pkill -f '^chrome'
The ^chrome
regular expression ensures that only processes starting with “chrome” are targeted.
pkill
also allows targeting processes based on their owner’s UID. This is useful when managing processes run by a specific user:
pkill -u <username>
Replace <username>
with the actual username. This will kill all processes owned by that user. Remember to replace <username>
with the actual username. You can use your UID instead of username like this:
pkill -U <UID>
You can combine options for highly specific process termination. For instance, to kill all processes owned by user “john” that match a specific pattern:
pkill -u john -f 'my_process'
This command kills processes owned by “john” and whose names contain “my_process”.
pkill
typically returns an exit code indicating success or failure. You can check the exit code to determine if the command was successful or not.
SIGKILL
: Use SIGKILL
only when necessary, as it can lead to data corruption.sudo
).This guide provides a foundation for utilizing pkill
effectively. Experiment with different options and regular expressions to master this powerful tool for Linux process management. Remember to always exercise caution when terminating processes, especially those you are unsure about.