Suppose several instances of a command are being executed. In such a scenario, we need the PID of each process. Both the ps and pgrep command return this information:
$ ps -C COMMAND_NAME
Alternatively, the following is returned:
$ ps -C COMMAND_NAME -o pid=
When = is appended to pid, it removes the header PID from the output of ps. To remove headers from a column, append = to the parameter.
This command lists the process IDs of Bash processes:
$ ps -C bash -o pid=
1255
1680
The pgrep command also returns a list of process IDs for a command:
$ pgrep bash
1255
1680
pgrep requires only a portion of the command name as its input argument to extract a Bash command; pgrep ash or pgrep bas will also work, for example. But ps requires you to type the exact command. pgrep supports these output-filtering options.
The -d option specifies an output delimiter other than the default new line:
$ pgrep COMMAND -d DELIMITER_STRING
$ pgrep bash -d ":"
1255:1680
The -u option filters for a list of users:
$ pgrep -u root,slynux COMMAND
In this command, root and slynux are users.
The -c option returns the count of matching processes:
$ pgrep -c COMMAND