0

By using ps aux | grep -i “name of your desired program” the list of PID's appeared, but I found more PIDs than in the System Monitor.

How is this possible?

I did not find PIDs with -color =auto in System Monitor.

muru
  • 197,895
  • 55
  • 485
  • 740
  • 2
    It would help if you would edit your question and include an example of the output you have a question about. The process count of the applications I have checked using your formula works as expected. – L. D. James Feb 01 '17 at 14:57

1 Answers1

6

When you run ps ... | grep ..., both ps and grep are started together, and the output of ps is fed to grep asynchronously. So, by the time ps scans the list of processes and prints the output, the grep process is also active, and the output of ps includes that grep process as well.

Now, if you do a simple grep foo, the output of ps will contain grep foo, and grep will match that foo:

$ ps aux | grep non-existent
muru  19042  0.0  0.0  10760  2224 pts/8    S+   23:56   0:00 grep non-existent

Obviously, there's no process named non-existent.

Instead of ps | grep, use pgrep for cleaner matching:

pgrep foo

Or ps itself, if you know the name of the command:

ps -C foo

Why grep --color...? Because Ubuntu defines an alias for grep by default:

$ alias grep
alias grep='grep --color=auto'

This is also why you see silly tricks like:

ps ... | grep foo | grep -v grep
ps ... | grep '[f]oo'
muru
  • 197,895
  • 55
  • 485
  • 740
  • I'm a bit new to linux, can i know what foo is? Isn't it just a user defined variable? Or does it have any function.(googled it but not yet found a google explanation). – nooneswarup Feb 01 '17 at 15:15
  • 1
    @user648595 it's anything you want it to be: https://en.wikipedia.org/wiki/Foobar – muru Feb 01 '17 at 15:16