Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

pkill

Find processes by name or other attributes (pgrep), signal them (pkill), or wait for them to end (pidwait).

Level
RHCSA exam
Group
Processes and jobs
Needs root
For changes
Package
procps-ng
Also called
pgrep, pidwait

What it does

pgrep PATTERN prints the PIDs of processes whose name matches the pattern (a regular expression). pkill PATTERN sends them a signal (TERM by default). Both select with the same options: by user (-u), terminal (-t), parent (-P), full command line (-f) and more.

Run pgrep first with the same options to see what pkill would hit.

How to write it

pgrep [OPTION]... PATTERN
pkill [OPTION]... [-SIGNAL] PATTERN
pidwait [OPTION]... PATTERN

Options 32

Most used

Signal (pkill)

-SIGNAL, --signal SIGNAL
The signal to send (default TERM).
-e, --echo
Show what was killed.
-q, --queue VALUE
Send a value with the signal.
-H, --require-handler
Only processes that handle the signal.

Output (pgrep)

-l, --list-name
Show the name with the PID.
-a, --list-full
Show the full command line.
-c, --count
Only the number of matches.
-d, --delimiter TEXT
Separator between PIDs.
-w, --lightweight
Show thread IDs.

Select

-f, --full
Match the whole command line, not only the name.
-u, --euid USERS
Effective user.
-U, --uid USERS
Real user.
-G, --group GIDS
Real group.
-g, --pgroup PGRPS
Process group.
-s, --session SIDS
Session.
-t, --terminal TERMS
Terminal, like pts/1.
-P, --parent PPIDS
Children of these parents.
-n, --newest
Only the newest match.
-o, --oldest
Only the oldest match.
-O, --older SECONDS
Only processes older than this.
-x, --exact
The name must match exactly.
-i, --ignore-case
Ignore case.
-v, --inverse
Everything that does not match.
-r, --runstates STATES
Only these states (D, R, S, Z …).
-A, --ignore-ancestors
Leave out the own parents.
-F, --pidfile FILE
Read PIDs from a file.
-L, --logpidfile
Fail if the PID file is not locked.
--cgroup NAMES
Only these cgroups.
--ns PID
Same namespaces as PID.
--nslist NAMES
Which namespaces to compare.
-h, --help
Show help.
-V, --version
Show the version.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Find PIDs by name

    pgrep -l sshd
    1010 sshd
    2210 sshd

    -l adds the name.

  2. 2See full command lines

    pgrep -a python3

    Find which script is running.

  3. 3Stop all processes of a user

    pkill -u olduser

    Before you delete the account.

  4. 4Log out a session

    pkill -KILL -t pts/3

    Ends everything on that terminal.

  5. 5Match the full command line

    pgrep -af 'backup.sh'
    pkill -f 'backup.sh'

    The process name is bash; -f finds the script.

  6. 6Count processes

    pgrep -c httpd
    6

    Quick health check.

  7. 7Wait until a process is gone

    pidwait -f backup.sh; echo "backup finished"

    Then start the next step.

  8. 8Reload a daemon by name

    pkill -HUP rsyslogd

    Re-reads the config.

  9. 9The newest or oldest match

    pgrep -n firefox; pgrep -o httpd

    -o httpd = the parent of all httpd children.

  10. 10Hunt processes that run from /tmp (security)

    pgrep -af '/tmp/|/dev/shm/'

    Suspicious locations for running programs.

  11. 11Show what pkill did

    pkill -e dd
    dd killed (pid 4123)

    Proof of the action.

Watch out

  • The pattern is a regular expression and matches part of the name: pkill ssh also hits ssh-agent and sshd. Use -x for an exact name.
  • Test with pgrep (same options) before pkill.

Exit codes

0At least one process matched.
1No process matched.
2Syntax error.
3Fatal error.