Linux has various command line utilities to handle the operations of the Operating System. One of these useful commands is “pgrep,” utilizing which the process IDs of the running program in the current system’s state could be found. The user can view a partial or complete process name and other related information.
The pgrep command is quite like the grep command but provides the user with much more information when compared and comes pre-installed in almost every other Linux distribution.
This article’s writing will teach our audience about the pgrep command in Linux, where we will learn the following.
- How Does the pgrep Command Work in Linux?
- How to Use the pgrep Command in Linux?
- Example 1: Viewing PID of a Given Running Program
- Example 2: Adding Delimiters to the Output
- Example 3: Listing the Process ID With Their Names
- Example 4: Listing the Process ID using a Full Command Line Flag
- Example 5: View the Oldest Running Process in the System
Let’s start discussing these methods in detail in the next sections.
How Does the pgrep Command Work in Linux?
The pgrep command comes in handy when the users want to know more about the current process IDs and can use it in the following syntax.
pgrep [OPTIONS] <PATTERN>
Here, the <PATTERN> represents the extended regular expressions, and you can get a list of all available options by executing this command.
$ pgrep -h
Following the options mentioned above will allow you to make use of the pgrep command in a variety of scenarios.
How to Use the pgrep Command in Linux?
Here, the usage and functionality of the pgrep command are provided through examples.
Example 1: Viewing PID of a Given Running Program
This command requires execution to view the PID of a program loaded in the memory which means that it is running:
$ pgrep firefox
Here 4412 represents the process id of the firefox application.
Example 2: Adding Delimiters to the Output
The delimiters are used to split the text from one entity to another, and while viewing the process, IDs using them could be a mess because the list is usually long, as you can see in this image.
$ pgrep -u itslinux
So, to avoid the lengthy scrolling procedure, we’d use this command to add more columns separated by a custom character (we’ll use ‘:’ as a delimiter.):
$ pgrep -u itslinux -d:
Example 3: Listing the Process ID With Their Names
Only seeing the IDs of running processes could be confusing for some users, so to get rid of it, this command should be used to list the process IDs along with the name of the process:
$ pgrep -u <USERNAME> -l
Example 4: Listing the Process ID using a Full Command Line Flag
This command is used to list the IDs of currently running programs along with the full command line:
$ pgrep -u itslinux -a
Example 5: View the Oldest Running Process in the System
Some users might be interested in knowing the first or oldest process when the system boots up; this command can be done using it.
$ pgrep -u itslinux -o -l
Pro Tip: If any function is not behaving properly and you are unable to close it, then there is a command that you can use to forcefully close the process as shown below:
$ kill 2505
We don’t recommend killing the processes until you know what you are doing, as doing something which wasn’t supposed to happen may crash the system losing all the unsaved data.
The pgrep command is quite helpful for programmers or scripters when they need to manage processes in Linux. This command works in a similar manner just like the grep command, but it provides additional information as well. This article has provided detailed insight into the pgrep command, along with some of its most commonly used flags.