pgrep and pkill: A Better killall?


Posted on Monday, November 2, 2015 by Charles Beynon
× Charles Beynon
[Photo: Dark-haired caucasian male, mid-thirties, wearing dark-rimmed eyeglasses]

Full Stack Developer mostly working in Rails. Recovering sysadmin. I like to dabble and tend to ramble. Don't say I didn't warn you ;D

About me

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
70376

Using the -l switch shows the process name as well

$ pgrep Finder
463
488
$ 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
463

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! 


Comments