Linux pgrep and pkill command

Updated: 04/26/2017 by Computer Hope

About pgrep and pkill

pgrep and pkill are used to look up (or signal) processes, based on name and other attributes.

Description

pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout. All the criteria have to match. For example,

pgrep -u root ssh

...will only list the processes that are called sshd and are owned by root. On the other hand,

pgrep -u root,daemon

...will list the processes owned by root OR daemon.

pkill will send the specified signal (by default SIGTERM) to each process instead of listing them on standard output.

pgrep and pkill syntax

pgrep [options] pattern
pkill [options] pattern

Options

-signal, --signal signal (pkill only.) Defines the signal to send to each matched process. Either the numeric or the symbolic signal name can be used.
-c, --count (pgrep only.) Suppress normal output; instead print a count of matching processes. When count does not match anything, e.g. returns zero, the command will return non-zero value.
-d, --delimiter delimiter (pgrep only.) Sets the string used to delimit each process ID in the output (by default a newline).
-f, --full The pattern is normally only matched against the process name. When -f is set, the full command line is used.
-g, --pgroup pgrp,... Only match processes in the process group IDs listed. Process group 0 is translated into pgrep's or pkill's own process group.
-G, --group gid,... Only match processes whose real group ID is listed. Either the numerical or symbolical value may be used.
-l, --list-name (pgrep only.) List the process name as well as the process ID.
-n, --newest Select only the newest (most recently started) of the matching processes.
-o, --oldest Select only the oldest (least recently started) of the matching processes.
-P, --parent ppid,... Only match processes whose parent process ID is listed.
-s, --session sid,... Only match processes whose process session ID is listed. Session ID 0 is translated into pgrep's or pkill's own session ID.
-t, --terminal term,... Only match processes whose controlling terminal is listed. The terminal name should be specified without the "/dev/" prefix.
-u, --euid euid,... Only match processes whose effective user ID is listed. Either the numerical or symbolical value may be used.
-U, --uid uid,... Only match processes whose real user ID is listed. Either the numerical or symbolical value may be used.
-v, --inverse Negates the matching. This option usually used in pgrep context. In pkill context the short option is disabled to avoid accidental usage of the option.
-x, --exact Only match processes whose name (or command line if -f is specified) exactly match the pattern.
-F, --pidfile file Read PID's from file. This option is perhaps more useful for pkill than pgrep.
-L, --logpidfile Fail if pidfile (see -F) not locked.
-V, --version Display version information and exit.
-h, --help Display a help message and exit.
pattern Specifies an Extended Regular Expression for matching against the process names or command lines.

pgrep and pkill examples

pgrep -u root named

Find the process ID of the named (name daemon) process.

pkill -HUP syslogd

Send the HUP signal to syslogd, which forces it to re-read its configuration file.

renice +4 $(pgrep firefox)

Make all firefox processes run nicer by a value of 4. This command illustrates the way pgrep's output can be passed to other utilities as input. In this case, the command pgrep firefox is passed as an argument to renice because it is enclosed in $( ).

ps — Report the status of a process or processes.
killall — Kill processes by name.
kill — Send a signal to a process, affecting its behavior or killing it.