A Handy Alias for Grepping Processes

unix
Posted on: 2014-10-31

Sometimes I want to see any running processes that match a given string. pgrep doesn’t quite give what I want. ps -ef | grep 'foo' is closer, but throws out the first line, which has the labels.

Lately I’m using this alias, which I found on the Unix StackExchange:

# Grep processes and show headers.
# See http://unix.stackexchange.com/a/47923/8514
function psgrep(){ ps -ef | sed -e '1p' -e "/$1/!d" }

(I think the -ef options are OSX-specific; man ps and choose accordingly.)

I use it like:

psgrep 'ruby'

The output is something like this:

UID   PID  PPID   C STIME   TTY           TIME CMD
123 34061 34034   0  2:47PM ttys007    0:03.21 ruby ruby_script_a.rb
123 34129 34120   0  2:49PM ttys008    0:01.23 ruby ruby_script_b.rb

I make commands like this readily-available by adding them to my shell aliases. See my dotfiles for examples.