Frequently Asked Question
What do pgrep and pkill do that ps | grep | kill doesn't?
pgrep and pkill are purpose-built tools from the procps-ng suite that walk
/proc directly, looking for processes whose name, command line, user, terminal,
or other attribute matches a pattern. pgrep nginx prints the PIDs of every
nginx process; pgrep -a nginx adds the command line; pgrep -u alice finds
every process owned by alice; pgrep -f "python myscript" matches the full
command line, not just the program name.
pkill is identical to pgrep but sends a signal instead of printing, pkill nginx is the same as kill $(pgrep nginx), plus correct quoting, proper exit
codes, and the crucial property that it won't accidentally match the grep
process you would otherwise have piped through. Both accept the usual signal
flags (pkill -9 firefox, pkill -HUP nginx). Compared with ps -ef | grep … | awk '{print $2}' | xargs kill, they are shorter, safer, and scriptable.