pkillFind or signal processes by pattern (pgrep / pkill)
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.
pkill [-SIGNAL] [-f] [-x] [-u USER] [-P PPID] [-n|-o] PATTERN pgrep [-a|-l] [-c] ... PATTERNYou want the PIDs of the report workers, cleanly, to use in another command.
pkillprocps-ng pkill: same matching as pgrep, but sends a signal to every match.
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 ...'.
-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)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 nodepgrep sh to test whether a shell is runningPatterns are substring regexes: sh matches bash, sshd, zsh, and anything else containing 'sh'.
pgrep -x shps aux | grep app | awk '{print $2}' | xargs killMatches 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 matchedIt's silent; only exit status 1 tells you.
pkill -x myapp || echo 'myapp was not running'Try these on a Linux machine of your own. Each task has a hint and a solution.
Start `sleep 700 &` and `sleep 800 &`. Print the PID and full command line of only the 800-second one.
Count how many bash processes you are running.
Stop both sleep processes with one pkill, printing what was killed.
Write a one-liner that prints 'running' if a process named cron exists and 'stopped' otherwise.
pkillFind or signal processes by pattern (pgrep / pkill)
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.
pkill [-SIGNAL] [-f] [-x] [-u USER] [-P PPID] [-n|-o] PATTERN pgrep [-a|-l] [-c] ... PATTERNYou want the PIDs of the report workers, cleanly, to use in another command.
pkillprocps-ng pkill: same matching as pgrep, but sends a signal to every match.
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 ...'.
-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)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 nodepgrep sh to test whether a shell is runningPatterns are substring regexes: sh matches bash, sshd, zsh, and anything else containing 'sh'.
pgrep -x shps aux | grep app | awk '{print $2}' | xargs killMatches 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 matchedIt's silent; only exit status 1 tells you.
pkill -x myapp || echo 'myapp was not running'Try these on a Linux machine of your own. Each task has a hint and a solution.
Start `sleep 700 &` and `sleep 800 &`. Print the PID and full command line of only the 800-second one.
Count how many bash processes you are running.
Stop both sleep processes with one pkill, printing what was killed.
Write a one-liner that prints 'running' if a process named cron exists and 'stopped' otherwise.