I used to wonder how can I find all the PID’s used by a specific user in the system or an application. This is a long time question in my mind. I used to try all sorts of commands to find what process is running by a service/user. The workaround commands which I used are as follows.
1.#ps -ef 2.#top
But none of these tools gives a number of processes run for an Application or a User.
That’s where I come across one wonderful command. To serve the purpose there is a command in Linux called pgrep(process grep). The grep command greps content in a file whereas pgrep greps process from proc file system. Let us have a look at pgrep usage.
Syntax of pgrep command in Linux
#pgrep application-name
Example1: Suppose we want to find what PID’s are used by firefox application.
# pgrep firefox
Output:
4000 4008
Example2: Suppose you need process run by a user then use below command
#pgrep -u user-ID
Example:
#pgrep -u 1000
Where 1000 is the user ID for a user
Sample output
1602 1621 1651 1654 1655 1660 1663 1669 1673 1678 1685 1686 1688
Example3: The above output just gives PID’s one each in the line. How about giving process details as well? We can use -a to list the process details as well.
Example:
root@linuxnix:~# pgrep nginx -a 30615 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; 30618 nginx: worker process
Example4: To list only PID’s but with different separator other than the new line, we can use -d to specify the delimiter.
Example:
root@linuxnix:~# pgrep -U 0 -d, 1,2,3,5,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,28,29,30,31,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,68,81,82,121,122,123,124,125,126,127,128,129,130,131,485,516,537,538,618,626,629,642,646,648,650,651,656,665,697,748,771,1274,1275,1280,1283,1297,1307,1310,1312,1342,1367,1407,1413,1534,1691,2889,4044,4046,4051,4105,4121,4131,4141,4174,4290,4332,23962,23963,30615,30629,31150
Example5: If we just want to list a number of the process used by a user instead of listing all the PID we can use count option.
Example:
root@linuxnix:~# pgrep -U 0 -c 103
And There are many options for this command explore your self by using man pages.
#man pgrep
In our next post, we will see about other process related command called pmap.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018