Viewing Processes with the pgrep command

Mosy sysadmins use the grep command along with ps to restrict the displayed output to certain processes. Solaris 10 offers the pgrep command to handle such situations.

The pgrep command lets you specify the criteria and then displays the information only about the processes that match the criteria.

The syntax for the pgrep command is shown here:

   pgrep [<options>] [<pattern>]

For example, the following command will select all the processes whose real group name is dba or oinstall:

$ pgrep -G dba,oinstall

You can also specify multiple criteria, and a logical AND will be assumed between the criteria. For example, consider the following command:

$ pgrep -G dba,oinstall -U oracle,mchurchi

This command will select the processes that match the following criteria:

(group name is dba OR oinstall) AND (user name is oracle OR mchurchi)

Now that you have a handle on how the pgrep command works, let's look at some of its options:

  • -d <delim> Specify delimiter string to be used to separate process IDs in the output. The newline character is the default.
  • -f The regular expression pattern should be matched against the full process argument string, which can be obtained from the pr_psargs field of the /proc/nnnnn/psinfo file.
  • -g <pgrpList> Select only those processes whose effective process group ID is in the list specified by <pgrpList>. If group 0 is included in the list, this is interpreted as the process group ID of the pgrep or pkill process.
  • -G <gidList> Select only those processes whose real group ID is in the list specified by <gidlist>.
  • -l Use the long output format.
  • -n Select only the newest (i.e., the most recently created) process that meets all other specified matching criteria. Can't be used with the -o option.
  • -o Matches only the oldest (i.e., the earliest created) process that meets all other specified matching criteria. Cannot be used with the -n option.
  • -P <ppidList> Select only those processes whose parent process ID is in the list specified by <ppidList>.
  • -s <sidList> Select only those processes whose process session ID is in the list specified by <sidList>. If ID 0 is included in the list, this is interpreted as the session ID of the pgrep or pkill process,
  • -t <termList> Select only those processes that are associated with a terminal in the list specified by <termList>.
  • -T <taskidList> Select only those processes whose task ID is in the list specified by . If ID 0 is included in the list, this is interpreted as the task ID of the pgrep or pkill process.
  • -u <euiList> Select only those processes whose effective user ID is in the list specified by <euiList>.
  • -U <uidlist> Select only those processes whose real user ID is in the list specified by <uidList>.
  • -v Reverse the matching logic—that is, select all processes except those which meet the specified matching criteria.
  • -x Select only those processes whose argument string or executable file name exactly matches the specified pattern—that is, all characters in the process argument string or executable file name must match the pattern.

Remember that in these commands, as anywhere else, a user ID may be specified either by the user name or by the numeric ID. This is also true for group IDs.

Like the ps command, pgrep displays a snapshot of the processes. The situation might have changed immediately after the output of these commands was displayed. In order to find the current information you will need to re-issue the command. If you want to monitor the processes continuously without having to reissue the command, use the prstat command