linux pkill/pgrep commands tutorial: pkill/pgrep syntax and examples

In operating systems such as Ubuntu and MAC, pkill and pgrep provide help manuals as a set of commands, which can be viewed through the man command.

pgrep, pkill – look up or signal processes based on name and other attributes

pgrep looks through the currently running processes and lists the process IDs which match the selection criteria to stdout.

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

Syntax

pgrep [options] pattern
pkill [options] pattern

Options

  • -signal
    –signal signal
    Defines the signal to send to each matched process. Either the numeric or the symbolic signal name can be used. (pkill only.)
  • -c, –count
    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
    Sets the string used to delimit each process ID in the output (by default a newline). (pgrep only.)
  • -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.
  • -i, –ignore-case
    Match processes case-insensitively.
  • -l, –list-name
    List the process name as well as the process ID. (pgrep only.)
  • -a, –list-full
    List the full command line as well as the process ID. (pgrep only.)
  • -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 is usually used in pgrep’s context. In pkill’s context the short option is disabled to avoid accidental usage of the option.
  • -w, –lightweight
    Shows all thread ids instead of pids in pgrep’s context. In pkill’s context this option is disabled.
  • -x, –exact
    Only match processes whose names (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.
  • –ns pid
    Match processes that belong to the same namespaces. Required to run as root to match processes from other users.
  • –nslist name,…
    Match only the provided namespaces. Available namespaces: ipc, mnt, net, pid, user,uts.
  • -V, –version
    Display version information and exit.
  • -h, –help
    Display help and exit.

Examples

List all processes whose names contain SSH and their process numbers

➜  ~ pgrep -l ssh
778 sshd
1859 ssh-agent
3560 sshd
3607 sshd
12717 sshd
12774 sshd
13314 sshd
13368 sshd
13513 sshd
13571 sshd

# The following command can achieve the same effect of pgrep.

➜  ~ ps -ef | grep 'ssh' | grep -v grep  | awk '{print $2, $8}'

List the processes with the smallest process ID in the SSH process name

➜  ~ pgrep -lo ssh
778 sshd

You can usually use this method to view the main process ID of a web server (such as nginx, httpd).

Kill all processes with a given partial name

➜  ~ pkill -f man

Of course, you can also use the following combined commands.

➜  ~ ps -ef | grep "man" | grep -v grep | awk '{print $2}' | xargs kill

Kill all processes of the specified user

➜  ~ pkill -u ylspirit

Kill all processes that do not belong to root

➜  ~ pkill -vu root

Kick user

➜  ~ pkill -kill -t pts/0

Kill all processes of the specified terminal

Add a Comment

Your email address will not be published. Required fields are marked *