How to get pgrep to display full process info

Solution 1:

pgrep's output options are pretty limited. You will almost certainly need to send it back through ps to get the important information out. You could automate this by using a bash function in your ~/.bashrc.

function ppgrep() { pgrep "$@" | xargs --no-run-if-empty ps fp; }

Then call the command with.

ppgrep <pattern>

Solution 2:

Combine pgrep with ps using xargs!

pgrep <your pgrep-criteria> | xargs ps <your ps options> -p

For example try

pgrep -u user | xargs ps -f -p

to get a full process list of user. Option -u user limits pgrep to the user given (as a number or name) while the ps options -f -p request a full format listing for the selected PID.

It's nice that you keep the first line with the column names. grep always drops the column names.


Solution 3:

The following only gives you PID + full command-line. For "all the info ps does", see other answers...

Most linuxes use procps-ng. Since 3.3.4 (released in 2012), pgrep -a (--list-full) shows the full command line.
Note: By default pgrep only matches the pattern you give against the executable name. If you want to match against the full command line (as grepping ps does), add the -f (--full) option.

In older versions (including the original procps project), -l option showed info but it's behavior varied:

  • pgrep -fl matched the pattern against full command line and showed the full command line.
  • pgrep -l alone matched only executable name and showed only executable name.
    If you don't want full match, you couldn't see the full command line :-( [https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=526355#15]

Not sure what code *BSD use but their man page documents the old -fl behavior.

Unfortunately you can't even use -fl portably - in recent procps-ng, -f (--list-name) always prints only the executable name.


Solution 4:

Linux

For the GNU version of pgrep long + fuzzy output is achieved with -af and the string must be case-sensitive (i.e. there is no option for case-insensitivity).

$ pgrep -af apache

OUTPUT:
    1748 /usr/sbin/apache2 -k start

Man page:

   -a, --list-full
       List  the  full  command line as well as the process ID.  (pgrep only.)

   -f, --full
       The pattern is normally only matched against the process name.  
       When -f is set, the full command  line is used.

MacOS

On OSX (and by inference, on BSD) -l (long output) in combination with -f (match against full argument lists) will display the complete command (-i adds case-insensitivity):

$ pgrep -fil ssh

OUTPUT:
    33770 ssh: [email protected] [mux] t

The man page:

 -l          Long output.  For pgrep, print the
             process name in addition to the
             process ID for each matching
             process.  If used in conjunction
             with -f, print the process ID and
             the full argument list for each
             matching process.  For pkill, dis-
             play the kill command used for
             each process killed.

Solution 5:

Use the -v option to grep - it returns everything BUT the requested pattern.

ps -ef | grep <process> | grep -v grep