Pgrep and Pkill Command Tutorial With Examples For Linux
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.
pgrep [options] pattern
pkill [options] pattern
$ 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
$ 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
--uid In this example we want ot filter
ssh term for process with where related user ID is
$ 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
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
$ 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
$ pgrep -a ssh
Up to now we have listed only matching processes and realted 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.
ismail@poftut1:~$ pgrep -v ssh
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 behaivour in a more strict way. With exact match only fully matched commands will be displayed. We will use
$ pgrep -x sshd
If there are multiple processes we can filter these process according to their age. We can only display newest process with
$ pgrep -n ssh
In previous example we have listed newest process. We can also show only oldest process with
$ pgrep -o ssh