pkill - Linux
Overview
The pkill
command in Linux allows users to send signals to processes based on their names and other attributes. It is primarily used to terminate or manage the state of running processes without needing to know their process IDs (PIDs). pkill
is extensively useful in administrative scripts, system shutdowns, and managing daemons or other background processes.
Syntax
The basic syntax for the pkill
command is as follows:
pkill [options] <pattern>
<pattern>
: Specifies the target process name or other attributes that match the processes to which the signal should be sent.
Options/Flags
-signal
,-<signal>
: Send a specific signal instead of the defaultTERM
. Examples include-HUP
,-KILL
,-CONT
, etc.-e
,--echo
: Display information about which processes were killed.-i
,--ignore-case
: Ignore case distinctions in the process name.-f
,--full
: Match against full argument lists instead of just the command name.-g
,--pgroup
: Target a specific process group.-n
,--newest
: Send the signal only to the newest process.-o
,--oldest
: Send the signal only to the oldest process.-P
,--parent
: Restrict targeting to children of the specified parent process.-u
,--euid
: Target processes owned by the specified user.-U
,--user
: Target processes whose real user ID matches the specified user.-v
,--inverse
: Reverse the match.
Examples
-
Send SIGTERM to a process by name:
pkill nginx
-
Case insensitive process termination:
pkill -i nodemon
-
Kill the newest instance of a specific process:
pkill -n firefox
-
Use the full argument list to match and terminate a script:
pkill -f 'python my_script.py'
Common Issues
- Incorrect signal names: Ensure signals are prefixed with a dash and are valid Linux signals.
- Processes not terminating: Some processes may ignore certain signals. Using
-KILL
(or-9
) can force termination, but use cautiously as this doesn’t allow process cleanup. - Permission issues: Running
pkill
without sufficient privileges to signal a process will fail. Use withsudo
when needed.
Integration
Combine pkill
with logging to manage process terminations:
pkill -f 'my_daemon' && echo "Daemon stopped successfully" >> /var/log/daemon.log
Or use it within scripts to control processes based on system events or load conditions.
Related Commands
kill
: Terminate processes by PID rather than by name.pgrep
: List PIDs matching a pattern, often used in conjunction withkill
.killall
: Similar topkill
, but traditionally works on older Unix systems.
For further reading and more detailed information, please consult the pkill
man page which can be accessed via man pkill
in your terminal.