Ubuntu – PID in Ubuntu gnome terminal

command lineprocessps

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.

Best Answer

  • 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'