Pgrep and Pkill Command Tutorial With Examples For Linux

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/pgrep-pkill-command-tutorial-examples-linux/
PINTEREST
PINTEREST
INSTAGRAM

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

Help

Help

Help

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.

List Process ID

List Process ID

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.

Print Process Count

Print Process Count

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.

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

Match For User ID

Match For User ID

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.

LEARN MORE  Ultimate Sed Tutorial With Examples

Match For Only Group Name

Match For Only Group Name

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 .

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.

Show Process Name

Show Process Name

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.

Show Full Command Line

Show Full Command Line

Inverse Match

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.

Inverse Match

Inverse Match

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 behaivour in a more strict way. With exact match only fully matched commands will be displayed. We will use -x .

Exact Match

Exact Match

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.

LEARN MORE  How To Exclude Hosts From Nmap Scan?

Show Newest

Show Newest

Show Oldest

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

Show Oldest

Show Oldest

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/pgrep-pkill-command-tutorial-examples-linux/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)