pgrep and pkill: A Better killall?

Posted on Monday, November 2, 2015 by Charles Beynon

Just learned a new tool that has somehow escaped me the last 15 years. prep is a simple tool that lets you grep through the names of running processes, returning just the PIDs. pkill takes that a step further, killing the matched processes1. The cool thing is that both support tab-completion under zsh.

Quick Examples

To find all running processes with the name jekyll

$ pgrep jekyll

Using the -l switch shows the process name as well

$ pgrep Finder
$ pgrep -l Finder
463 Finder
488 FinderSynExt

Note how without the -l switch we don’t know that we’re matching FinderSyncExt. Good thing we didn’t run pkill yet!

To match exactly, the -x switch is used. This can help solve the problem of matching too greedily, especially when using pkill.

$ pgrep -x Finder

Perhaps the too-greedy-by-default is why must instructions found on the web use killall over pkill. Still, I am liking the tab-completion offered by zsh.

  1. A note of warning: pkill will kill processes that match only partially. So pkill Finder will kill Finder and FinderSynExt processes. Be careful!