While working process general ps command is used. Ps command provides detailed information about processes those run on the system. We generally use grep command with ps to filter or match. There is a alternative and more practical way to work process. Pgrep and pkill can be used to manage, list and kill process.

Syntax

pgrep [options] pattern pkill [options] pattern

Help

Quick help about pkill command can be get with the -h option like below.

$ pkill -h

List Process ID

The simplest and common usage of pgrep is just providing some term to search in process list. In this example we will search for the term ssh in the process list.

$ pgrep ssh

We can see from screenshot that all process ID’s those contain ssh are listed line by line.

Print Process Count

The another usage for pgrep is counting related processes. We can use wc command to count lines but pgrep all ready provides this functionality with -c option. This will not list process ID’s and only list the count of the process ID’s.

$ pgrep -c ssh

Set Delimiter For Process List

If there are more than one process this processes ID’s will be listed line by line by default. There is an option to list PID’s by delimiting them specified delimiter. We can specify the delimiter with -d option. In this example we want to list PID’s with - delimiter.

$ pgrep -d "-" ssh

Match For User ID

Another useful feature is matching and filtering PID’s according to user ID. As we know each process have related user ID for security and permission issues. We can filter process according to this User ID with -U or --uid In this example we want ot filter ssh term for process with where related user ID is 1000

$ pgrep -U 1000 ssh

Match For Only Group Name

We can also specify the group name to filter processes. As we know process are created with relevant user and group name for security and permission reasons. We will use -G parameter. In this example we want to list group name ismail for ssh term in the process name.

$ pgrep -G ismail ssh

Match For Only Group ID

In previous example we have used names for group filter. We can also specify group ID with -g option. In the example we will filter for group ID 1000 .

$ pgrep -g 1000 ssh

Show Process Name

By default pgrep command will only list PID’s. But some times we may need to display the process name too. For this we will use -l option like below.

$ pgrep -l ssh

Show Full Command Line

In previous example we have only listed the command. So there will be no parameters or options about the command. We can list all information about the commands with -a option.

$ pgrep -a ssh

Inverse Match

Up to now we have listed only matching processes and related information. We can reverse the output. What this means is the output will be unmatched processes. We will use -v option. But there will be a lot of output lines.

Exact Match

By default term search is done in a relax mode. So if only some part of the command is matched the command is displayed. We can change the match behavior in a more strict way. With exact match only fully matched commands will be displayed. We will use -x .

$ pgrep -x sshd

Show Newest

If there are multiple processes we can filter these process according to their age. We can only display newest process with -n parameter.

$ pgrep -n ssh

Show Oldest

In previous example we have listed newest process. We can also show only oldest process with -o option

$ pgrep -o ssh