CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Process Management / pkill

pkill

Find or signal processes by pattern (pgrep / pkill)

Beginneressentialsignalsscripting

pgrep finds processes by name, full command line, user or parent and prints their PIDs; pkill takes the same arguments and signals them instead. Together they replace the fragile 'ps | grep | awk | xargs kill' pipeline in scripts.

Syntaxpkill [-SIGNAL] [-f] [-x] [-u USER] [-P PPID] [-n|-o] PATTERN pgrep [-a|-l] [-c] ... PATTERN

Watch it run

You want the PIDs of the report workers, cleanly, to use in another command.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
pgrep python3

By default pgrep matches the process name and prints only PIDs, one per line. No 'grep' line in the output, unlike ps | grep.

Anatomy of a command

$
commandpkill

procps-ng pkill: same matching as pgrep, but sends a signal to every match.

How it works

pgrep and pkill are the same procps-ng program. They read /proc, and for each PID compare your extended regular expression with the process name from /proc/PID/stat (comm, max 15 characters) or, with -f, with the full argument list from /proc/PID/cmdline. Matching is a substring search unless you anchor it or use -x. Filters (-u effective UID, -U real UID, -P parent PID, -t terminal, -n newest, -o oldest) narrow the set. pgrep then prints the PIDs; pkill calls kill(pid, sig) on each. Both skip their own process, but not the shell or ssh session that launched them, so pkill -f can match its caller. Exit status is 0 if something matched, 1 if nothing did, 2 for a syntax error: perfect for 'if pgrep -x nginx >/dev/null; then ...'.

Options you'll actually use

-a (pgrep)Print PID and full command line
-l (pgrep)Print PID and process name
-c (pgrep)Print only the number of matches
-fMatch against the full command line
-xExact match: the whole name (or command line with -f) must match
-u USEROnly processes with this effective user
-P PPIDOnly children of this parent PID
-n / -oOnly the newest / oldest match
-SIGNAL (pkill)Signal to send: -HUP, -9, --signal USR1
-e (pkill)Echo what was killed
-d DELIM (pgrep)Separator between PIDs (-d, for a comma list)

Common mistakes

pkill -f node

-f matches the substring anywhere in any command line: node_exporter, a vim editing node.conf, your own ssh command.

pkill -f '^node /home/kunal/app/server.js' or pkill -x node
pgrep sh to test whether a shell is running

Patterns are substring regexes: sh matches bash, sshd, zsh, and anything else containing 'sh'.

pgrep -x sh
ps aux | grep app | awk '{print $2}' | xargs kill

Matches the grep process itself and any line that mentions 'app', and fails noisily when nothing matches.

pkill -f 'app pattern'
Expecting pkill to print an error when nothing matched

It's silent; only exit status 1 tells you.

pkill -x myapp || echo 'myapp was not running'

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Start `sleep 700 &` and `sleep 800 &`. Print the PID and full command line of only the 800-second one.

  2. 2

    Count how many bash processes you are running.

  3. 3

    Stop both sleep processes with one pkill, printing what was killed.

  4. 4

    Write a one-liner that prints 'running' if a process named cron exists and 'stopped' otherwise.

Related commands

killSend a signal to a processkillallSignal every process with a given namepsSnapshot the running processes
PreviouskillallNextjobs
Linux / Process Management / pkill

pkill

Find or signal processes by pattern (pgrep / pkill)

Beginneressentialsignalsscripting

pgrep finds processes by name, full command line, user or parent and prints their PIDs; pkill takes the same arguments and signals them instead. Together they replace the fragile 'ps | grep | awk | xargs kill' pipeline in scripts.

Syntaxpkill [-SIGNAL] [-f] [-x] [-u USER] [-P PPID] [-n|-o] PATTERN pgrep [-a|-l] [-c] ... PATTERN

Watch it run

You want the PIDs of the report workers, cleanly, to use in another command.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
pgrep python3

By default pgrep matches the process name and prints only PIDs, one per line. No 'grep' line in the output, unlike ps | grep.

Anatomy of a command

$
commandpkill

procps-ng pkill: same matching as pgrep, but sends a signal to every match.

How it works

pgrep and pkill are the same procps-ng program. They read /proc, and for each PID compare your extended regular expression with the process name from /proc/PID/stat (comm, max 15 characters) or, with -f, with the full argument list from /proc/PID/cmdline. Matching is a substring search unless you anchor it or use -x. Filters (-u effective UID, -U real UID, -P parent PID, -t terminal, -n newest, -o oldest) narrow the set. pgrep then prints the PIDs; pkill calls kill(pid, sig) on each. Both skip their own process, but not the shell or ssh session that launched them, so pkill -f can match its caller. Exit status is 0 if something matched, 1 if nothing did, 2 for a syntax error: perfect for 'if pgrep -x nginx >/dev/null; then ...'.

Options you'll actually use

-a (pgrep)Print PID and full command line
-l (pgrep)Print PID and process name
-c (pgrep)Print only the number of matches
-fMatch against the full command line
-xExact match: the whole name (or command line with -f) must match
-u USEROnly processes with this effective user
-P PPIDOnly children of this parent PID
-n / -oOnly the newest / oldest match
-SIGNAL (pkill)Signal to send: -HUP, -9, --signal USR1
-e (pkill)Echo what was killed
-d DELIM (pgrep)Separator between PIDs (-d, for a comma list)

Common mistakes

pkill -f node

-f matches the substring anywhere in any command line: node_exporter, a vim editing node.conf, your own ssh command.

pkill -f '^node /home/kunal/app/server.js' or pkill -x node
pgrep sh to test whether a shell is running

Patterns are substring regexes: sh matches bash, sshd, zsh, and anything else containing 'sh'.

pgrep -x sh
ps aux | grep app | awk '{print $2}' | xargs kill

Matches the grep process itself and any line that mentions 'app', and fails noisily when nothing matches.

pkill -f 'app pattern'
Expecting pkill to print an error when nothing matched

It's silent; only exit status 1 tells you.

pkill -x myapp || echo 'myapp was not running'

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Start `sleep 700 &` and `sleep 800 &`. Print the PID and full command line of only the 800-second one.

  2. 2

    Count how many bash processes you are running.

  3. 3

    Stop both sleep processes with one pkill, printing what was killed.

  4. 4

    Write a one-liner that prints 'running' if a process named cron exists and 'stopped' otherwise.

Related commands

killSend a signal to a processkillallSignal every process with a given namepsSnapshot the running processes
PreviouskillallNextjobs