I've just tried the following command on my Ubuntu, it doesn't show a thing:
pgrep php5
shouldn't it return the process id of php5(which the following command just does)?:
ps aux | grep php5
So, what's the difference between these two commands?
05 Answers
The ps aux | grep x command gives "better" results than pgrep x essentially because you are missing an option with the latter.
Simply use the -f option for pgrep to search the full command line and not only the process name which is its default behavior, eg:
pgrep -f php5Unlike the ps | grep construction with which you need to filter out the grep line or use pattern tricks, pgrep just won't pick itself by design.
Moreover, should your pattern appear in ps USER column, you'll get unwanted processes in the output, pgrep doesn't suffer from this flaw.
If you want full details instead of just the pids, you can use:
ps wup $(pgrep -f python)which is simpler and more reliable than
ps aux | grep python | grep -v grepor
ps aux | grep p[y]thon 6 ps aux includes the full command line (path and parameters), while pgrep only looks at the first 15 characters of the executable's names
ps aux returns the full command line of each process, while pgrep only looks at the names of the executables.
That means that grepping ps aux output will match anything that occurs in the path or the parameters of a process' binary: e.g. `
ps aux | grep php5will match/usr/share/php5/i-am-a-perl-script.pl- but
pgrep php5won't
Take an example from my system -- only we'll use python instead of php5:
ps aux | grep pythongives us:
izx 2348 0.0 0.7 514928 15644 ? Sl Jun24 0:00 /usr/bin/python /usr/lib/unity-lens-video/unity-lens-video izx 2444 0.0 0.9 547392 18864 ? Sl Jun24 0:01 /usr/bin/python /usr/lib/unity-scope-video-remote/unity-scope-video-remote root 2805 0.0 0.5 95436 12204 ? S Jun24 0:00 /usr/bin/python /usr/lib/system-service/system-service-d izx 6272 0.0 2.9 664400 60320 ? SNl Jun24 1:16 /usr/bin/python /usr/bin/update-manager --no-focus-on-map root 11729 0.0 0.9 180508 19516 ? S Jun25 0:00 python /usr/lib/software-properties/software-properties-dbus
- But
pgrep pythonreturns only11729, which you'll see from the above list is:
root 11729 0.0 0.9 180508 19516 ? S Jun25 0:00 python /usr/lib/software-properties/software-properties-dbus7
diff <(ps aux|grep x) <(pgrep x) # :) 1 At this time, ps will give more complete output than pgep -f as pgrep is limited to the first 4,096 characters (often affecting Java users looking for the entry class of a Java program with a long classpath). The bug tracking this is:
It may not be better... when I tried to look for the tmux server process,
pgrep -l tmuxshowed it, but
ps aux | grep tmux won't show it as the server but shows it as the command that triggered the start of the server. (tmux new -s foo)