Skip to main content

Process Commands

pgrep Command Examples

Want to look for a process and its details? The pgrep command helps you with that. Here's how to use it.

Like the grep command used to find strings from files and output, the pgrep command does the same for the processes.

In simple terms, the pgrep command will get you the PID of running processes.

So in this guide, I will walk you through various examples of how you can use the pgrep command.

Using the pgrep command on Linux

To use the pgrep command, you will have to follow the given command syntax:

pgrep [options] pattern

Here, the pattern is where you will specify the parameters for the output.

Now, let's have a look at some examples.

Find the PID of a specific process using the pgrep command

To find the PID of a specific process, all you have to do is follow the given syntax:

pgrep -u username process_name

So let's say I want to find the process ID of ssh for the user sagar so I will be using the following command:

pgrep -u sagar ssh
get the PID of specific service using the pgrep command on linux

Get the number of processes owned by the user

If you want to know the number of processes being utilized by the specific user, append the username with -c and -u flag to the pgrep command as shown:

pgrep -c -u username

For example, here, I looked for the number of processes owned by a user named sagar:

pgrep -c -u sagar
get the number of processes owned by user on linux using the pgrep command

And if you want to combine the processes count of multiple users, you can separate them by column:

pgrep -c -u user-1,user-2

For example, here, I combined the processes of two users: sagar and root:

get process count of multiple users on linux using the pgrep command

Search processes in a case-insensitive manner

By default, the pgrep utility operates in a case-sensitive mode so you may not be able to find the desired processes.

But you can switch to the case-insensitive mode using the -i flag:

pgrep -i process_name

For example, here, I looked for firefox but used all capitals and it returned the right result:

pgrep -i FIREFOX
Search processes in a case insensitive manner using the pgrep command on linux

List ongoing processes with their names and PIDs

If you want to list every ongoing process with their names and PID, it can easily be done using the -l flag:

pgrep -u username -l

For example, here I listed every ongoing process relative to the user named sagar:

pgrep -u sagar -l
List ongoing processes with their names and PIDs using the pgrep command on linux

Find which command was used to start the ongoing process

The pgrep utility also allows you to see the command used to start the ongoing process by using the -a flag:

pgrep -u username -a

So let's say I'm looking for commands that executed ongoing processes for user root, then the following command should get my job done:

pgrep -u root -a
Find which command was used to start the ongoing process in linux

Find the most recently started process

If you want to find the most recent ongoing process, it can easily be found using the -n flag:

pgrep -n 

But I would recommend pairing -n with -l to have more useful output as it will add use process name too:

pgrep -n -u sagar -l
find the most recent ongoing process on linux using the pgrep command

Find the oldest ongoing process on Linux

In case, if you want to find the oldest ongoing process on your system, the pgrep utility can do that when executed with -o flag:

pgrep -o

For example, if I want to find the oldest ongoing process for my user (sagar), I will be using the following command:

pgrep -u sagar -o -l
Find the oldest ongoing process using the pgrep command

But how about killing unnecessary processes?

Once you find out the set of unnecessary processes, you can use the various signals to kill them.

Don't know how to? Well, we have a detailed guide on how to kill processes from the terminal in Linux:

How to Use Kill Command in Linux?
Found a misbehaving process? Here’s how to teach a lesson to it by terminating it using various commands.

And if you are interested in learning more about signals to terminate the process, we have your back:

How to use SIGINT and other Termination Signals in Linux
Terminating executing process is more than just kill -9. Here are some of the prominent termination signals and their usage.

I hope you will find the given guide helpful and if you have any queries or suggestions, feel free to let me know in the comments.