1. Overview

In Linux, pgrep is a utility that can search for processes based on specified criteria, such as process name, user ID, terminal ID, and so on.

In this tutorial, we’ll explore multiple ways to retrieve process IDs using the pgrep command. Though we’ve tested each approach with version 3.3.17 of pgrep on Ubuntu Linux, they should work fine on most other Linux distributions without any modification.

2. Effectiveness Over Traditional Tools

Let’s say we have multiple Vim sessions on our system and want to get the process IDs for each one.

Using the ps command, we can list all processes and filter them with grep:

$ ps -elf | grep vim | grep -v grep
4 T root        62    13  0  80   0 -  4364 -      05:45 pts/0    00:00:00 vim sample.txt
4 T root       128    13  0  80   0 -  4375 -      07:13 pts/0    00:00:00 vim sample2.txt
4 T root       129    13  0  80   0 -  4375 -      07:13 pts/0    00:00:00 vim sample3.txt
4 T root       130    13  0  80   0 -  4375 -      07:13 pts/0    00:00:00 vim sample4.txt
4 T tavasthi   170   168  0  80   0 -  4317 do_sig 07:18 pts/0    00:00:00 vim sample5.txt
4 T tavasthi   171   168  0  80   0 -  4317 do_sig 07:18 pts/0    00:00:00 vim sample6.txt
4 T tavasthi   220   218  0  80   0 -  4317 do_sig 07:25 pts/4    00:00:00 vim demo1.txt
4 T tavasthi   221   218  0  80   0 -  4317 do_sig 07:25 pts/4    00:00:00 vim demo2.txt

Further, we can use awk to extract the process ID from the fourth column of each output record:

$ ps -elf | grep vim | grep -v grep | awk '{print $4}'
62
128
129
130
170
171
220
221

Until the availability of the pgrep utility, this was the typical way to extract the process ID. However, we can instead get this information with the pgrep command with ease:

$ pgrep vim
62
128
129
130
170
171
220
221

Lastly, it’s important to note that pgrep is usually faster at extracting process ID based on specific criteria than the traditional method.

3. Customizing Output Format

The default behavior of the pgrep command limits the output to process ID. In this section, we’ll learn a few pgrep options that we can use to customize the output.

3.1. The ‐‐delimiter (-d) Option

pgrep uses the newline character (\n) to separate multiple process IDs in the default output. However, we can use the ‐‐delimiter option to specify a custom delimiter.

Let’s see this in action by separating the process IDs with a comma (,):

$ pgrep --delimiter ',' vim
62,128,129,130,170,171,220,221

Further, we can also specify a multi-line delimiter string to tabulate the output:

$ pgrep --delimiter "
---
" vim
62
---
128
---
129
---
130
---
170
---
171
---
220
---
221
---
262

We got the expected output. However, we should be good with a single character, such as a comma or tab, for most common use cases.

3.2. The ‐‐list-name (-l) Option

We can use the ‐‐list-name option if we want to show the name of the process along with its process ID. Let’s use this option to show all the processes corresponding to our Vim sessions:

$ pgrep --list-name vim
62 vim
128 vim
129 vim
130 vim
170 vim
171 vim
220 vim
221 vim

We must note that all the Vim sessions have the same process name.

3.3. The ‐‐list-full (-a) Option

For our multiple Vim sessions scenario, we might want to know how to map the process ID with the filename. For this purpose, we can use the ‐‐list-full (-a) option to show the complete command-line:

$ pgrep --list-full vim
62 vim sample.txt
128 vim sample2.txt
129 vim sample3.txt
130 vim sample4.txt
170 vim sample5.txt
171 vim sample6.txt
220 vim demo1.txt
221 vim demo2.txt

Great! Now, we can easily see which file is used by each process.

3.4. The ‐‐count (-c) Option

We can also use the ‐‐count (-c) option if we only need to know the number of processes that match our search. Let’s use it to see the count of Vim sessions:

$ pgrep --count vim
8

We’ve got the expected count for the processes matching our search query.

4. Filtering

In this section, let’s learn some of the most useful filtering options available with pgrep.

4.1. By Process Name

We’ve already seen that we can filter the process IDs based on the process name:

$ pgrep vim
62
128
129
130
170
171
220
221

Further, it’s important to note that pgrep uses a case-sensitive search. So, if we use “viM” as a pattern to filter the processes, then we won’t get any output:

$ pgrep viM
$ echo $?
1

Now, let’s see how we can use the ‐‐ignore-case (-i) option to perform a case-insensitive search:

$ pgrep --ignore-case viM
62
128
129
130
170
171
220
221

It works as expected.

Lastly, we can use the ‐‐full (-f) option to match the processes with a regular expression matching the process command line. Let’s see this in action by filtering the Vim sessions based on filename patterns:

$ pgrep --list-full --full 'vim .*[3-4].txt'
129 vim sample3.txt
130 vim sample4.txt

Perfect! Our output contains two process IDs, and their command lines match our regular expression query.

4.2. By Recency

The output of the pgrep command will contain all the process IDs that match our search criteria. However, if we want to see only the oldest or most recent process, we can use the ‐‐oldest (-o) and ‐‐newest (-n) options.

First, let’s see the oldest Vim session by using the ‐‐oldest option:

$ pgrep --oldest vim
62

Now, let’s use the ‐‐newest option to get the most recent Vim session:

$ pgrep --newest vim
221

Lastly, we can use the ‐‐older (-O) option to show all processes older than a time duration in seconds. Let’s find out all the Vim sessions that are active for more than 10,000 seconds:

$ pgrep --older 10000 vim
62

Interestingly, only one Vim session matches our search criteria.

4.3. By Users

We can filter the processes running under a specific user account with the ‐‐uid (-u) option:

$ pgrep vim --uid tavasthi
170
171
220
221

Four Vim sessions are running under this user.

Additionally, we can provide a comma-separated list of user accounts to filter the processes running under any one of those accounts:

$ pgrep vim -u root,tavasthi
62
128
129
130
170
171
220
221

As expected, we got process IDs of Vim sessions running under either root or tavasthi user accounts.

4.4. By Terminal IDs

We can also identify the processes running on specific terminals using the ‐‐terminal (-t) option.

Let’s find out the process IDs for the Vim sessions running on the pts/0 terminal:

$ pgrep --list-full vim --terminal pts/0
62 vim sample.txt
128 vim sample2.txt
129 vim sample3.txt
130 vim sample4.txt
170 vim sample5.txt
171 vim sample6.txt

There are six Vim sessions on this terminal.

Now, let’s list down the ones running on the pts/4 terminal:

$ pgrep --list-full vim -t pts/4
220 vim demo1.txt
221 vim demo2.txt

It looks like we’ve got this one right.

4.5. By Parent Process IDs

We can identify the processes under the same parent process using the ‐‐parent (-P) option.

Let’s see this in action by checking the Vim sessions running under the parent process with PPID=13:

$ pgrep --list-full --parent 13 vim
62 vim sample.txt
128 vim sample2.txt
129 vim sample3.txt
130 vim sample4.txt

Additionally, we can provide a comma-separated list of PPIDs. Let’s list down the Vim sessions running under parent processes with PPID 13 or 218:

$ pgrep --list-full --parent 13,218 vim
62 vim sample.txt
128 vim sample2.txt
129 vim sample3.txt
130 vim sample4.txt
220 vim demo1.txt
221 vim demo2.txt

It looks like we’ve nailed this one.

5. Conclusion

In this article, we learned how to get the process IDs based on specified criteria using the pgrep command. Further, we explored multiple options for customizing the output, such as -d, -l, -a, and -c.

Lastly, we learned about the -i, -f, -o, -O, -u, -t, and -P options to filter the process IDs based on criteria such as name, recency, user ID, terminal ID, and process parent ID.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments