More elegant "ps aux | grep -v grep"

LinuxGrep

Linux Problem Overview


When I check list of processes and 'grep' out those that are interesting for me, the grep itself is also included in the results. For example, to list terminals:

$ ps aux  | grep terminal
user  2064  0.0  0.6 181452 26460 ?        Sl   Feb13   5:41 gnome-terminal --working-directory=..
user  2979  0.0  0.0   4192   796 pts/3    S+   11:07   0:00 grep --color=auto terminal

Normally I use ps aux | grep something | grep -v grep to get rid of the last entry... but it is not elegant :)

Do you have a more elegant hack to solve this issue (apart of wrapping all the command into a separate script, which is also not bad)

Linux Solutions


Solution 1 - Linux

The usual technique is this:

ps aux | egrep '[t]erminal'

This will match lines containing terminal, which egrep '[t]erminal' does not! It also works on many flavours of Unix.

Solution 2 - Linux

Use pgrep. It's more reliable.

Solution 3 - Linux

This answer builds upon a prior pgrep answer. It also builds upon another answer combining the use of ps with pgrep. Here are some pertinent training examples:

$ pgrep -lf sshd
1902 sshd

$ pgrep -f sshd
1902

$ ps up $(pgrep -f sshd)
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1902  0.0  0.1  82560  3580 ?        Ss   Oct20   0:00 /usr/sbin/sshd -D

$ ps up $(pgrep -f sshddd)
error: list of process IDs must follow p
[stderr output truncated]

$ ps up $(pgrep -f sshddd) 2>&-
[no output]

The above can be used as a function:

$ psgrep() { ps up $(pgrep -f $@) 2>&-; }

$ psgrep sshd
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1902  0.0  0.1  82560  3580 ?        Ss   Oct20   0:00 /usr/sbin/sshd -D

Compare with using ps with grep. The useful header row is not printed:

$  ps aux | grep [s]shd
root      1902  0.0  0.1  82560  3580 ?        Ss   Oct20   0:00 /usr/sbin/sshd -D

Solution 4 - Linux

You can filter in the ps command, e.g.

ps u -C gnome-terminal

(or search through /proc with find etc.)

Solution 5 - Linux

One more alternative:

ps -fC terminal

Here the options:

 -f	       does full-format listing. This option can be combined
	       with many other UNIX-style options to add additional
	       columns. It also causes the command arguments to be
	       printed. When used with -L, the NLWP (number of
	       threads) and LWP (thread ID) columns will be added. See
	       the c option, the format keyword args, and the format
	       keyword comm.

 -C cmdlist     Select by command name.
	            This selects the processes whose executable name is
	            given in cmdlist.

Solution 6 - Linux

Disclaimer: I'm the author of this tool, but...

I'd use px:

~ $ px atom
  PID COMMAND          USERNAME   CPU RAM COMMANDLINE
14321 crashpad_handler walles   0.01s  0% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Electron Framework.framework/Resources/crashpad_handler --database=
16575 crashpad_handler walles   0.01s  0% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Electron Framework.framework/Resources/crashpad_handler --database=
16573 Atom Helper      walles    0.5s  0% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Atom Helper.app/Contents/MacOS/Atom Helper --type=gpu-process --cha
16569 Atom             walles   2.84s  1% /Users/walles/Downloads/Atom.app/Contents/MacOS/Atom --executed-from=/Users/walles/src/goworkspace/src/github.com/github
16591 Atom Helper      walles   7.96s  2% /Users/walles/Downloads/Atom.app/Contents/Frameworks/Atom Helper.app/Contents/MacOS/Atom Helper --type=renderer --no-san

Except for finding processes with a sensible command line interface it also does a lot of other useful things, more details on the project page.

Works on Linux and OS X, easily installed:

curl -Ls https://github.com/walles/px/raw/python/install.sh | bash

Solution 7 - Linux

Using brackets to surround a character in the search pattern excludes the grep process since it doesn't contain the matching regex.

$ ps ax | grep 'syslogd'
   16   ??  Ss     0:09.43 /usr/sbin/syslogd
18108 s001  S+     0:00.00 grep syslogd

$ ps ax | grep '[s]yslogd'
   16   ??  Ss     0:09.43 /usr/sbin/syslogd

$ ps ax | grep '[s]yslogd|grep'
   16   ??  Ss     0:09.43 /usr/sbin/syslogd
18144 s001  S+     0:00.00 grep [s]yslogd|grep

Solution 8 - Linux

Depending on the ultimate use case, you often want to prefer Awk instead.

ps aux | awk '/[t]erminal/'

This is particularly true when you have something like

ps aux | grep '[t]erminal' | awk '{print $1}'  # useless use of grep!

where obviously the regex can be factored into the Awk script trivially:

ps aux | awk '/[t]erminal/ { print $1 }'

But really, don't reinvent this yourself. pgrep and friends have been around for a long time and handle this entire problem space much better than most ad hoc reimplementations.

Solution 9 - Linux

Another option is to edit your .bash_profile (or other file that you keep bash aliases in) to create a function that greps 'grep' out of the results.

function mygrep {
grep -v grep | grep --color=auto $1
}

alias grep='mygrep'

The grep -v grep has to be first otherwise your --color=auto won't work for some reason.

This works if you're using bash; if you're using a different shell YMMV.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJakub M.View Question on Stackoverflow
Solution 1 - LinuxJohnsywebView Answer on Stackoverflow
Solution 2 - LinuxDarkDustView Answer on Stackoverflow
Solution 3 - LinuxAsclepiusView Answer on Stackoverflow
Solution 4 - LinuxAndreas FrischeView Answer on Stackoverflow
Solution 5 - LinuxTaXXoRView Answer on Stackoverflow
Solution 6 - LinuxJohan WallesView Answer on Stackoverflow
Solution 7 - LinuxauntchiladaView Answer on Stackoverflow
Solution 8 - LinuxtripleeeView Answer on Stackoverflow
Solution 9 - LinuxFlyingCodeMonkeyView Answer on Stackoverflow