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 php5
Unlike 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 grep
or
ps aux | grep p[y]thon
-a
(--list-full
) option if you want to see the full command line and not just the pid. (Older pgrep had no-a
, did this on-fl
.) – Beni Cherniavsky-Paskin Aug 12 '14 at 08:51pgrep
to play nice the solution. +1 – 2rs2ts Jun 25 '15 at 21:32/proc/self/cmdline
to be "descriptive",pgrep -fa ruby
will not match eg.puma 3.3.0 (tcp://localhost:3000) [MIQ: Web Server Worker]
, while the "dumber"pgrep -a ruby
will. Not sure if the latter may be fooled too. – Beni Cherniavsky-Paskin May 01 '16 at 12:47pgrep
andps
. – Franklin Yu Jul 07 '16 at 06:13ps wup
is a nice suggestion. I'd recommend adding another 'w' making itps wwup ...
to ensure that the output doesn't get truncated to the width of the terminal – kshenoy Sep 20 '18 at 14:01