Linux is loved by developers and enthusiasts alike. You can do lots of things you usually can’t do on operating systems such as windows and OSX.
Linux is the best operating system for programmers, developers, and anyone who wants to understand operating systems properly. Now Linux has many functions and capabilities. Today you are going to learn about Linux pgrep and pkill command and how you can use them to make your Linux experience worthwhile. As name suggests pgrep command is used to search the process based on name and pkill command will terminate or kill the process based on the name.
- A Linux operating system (screenshots are from a centos / rhel machine)
Syntax: # pgrep [options] pattern
Most Linux users recognize the grep command which is used to find specific words or characters. pgrep works in almost the same way. The pgrep command is used while searching for processes (based on name) that are running on the system at the time of use.
Let’s deep dive into pgrep command examples,
Example:1) List Process ID owned by specific user (-u)
When used, pgrep lists the process IDs which match the criteria that you specify. For example, when you type;
[[email protected] ~]# pgrep -u apache httpd 1274 1275 1278 1282 1283 1284 1286 1291 [[email protected] ~]#
The system lists all processes called httpd that are owned by apache user.
Example:2) List all processes owned by multiple users
Let’s assume we want to list all processes owned by root and apache user, execute following command,
[[email protected] ~]# pgrep -u apache,root
Here users are separated by commas (,)
Example:3) Count Number of Matching Process (-c)
Let’s suppose we want to count number process owned by apache and root user, run the beneath command
[[email protected] ~]# pgrep -u apache -c 8 [[email protected] ~]# pgrep -u root -c 95 [[email protected] ~]#
In above command, ‘-c’ option is used to count number of process associated to a user.
Example:4) List Process IDs along with process name (-l & -a)
Let’s suppose we want to list process ids and process name associated to a user, then use -l or -a option in pgrep command. -l option will list only process name whereas -a will list full path of process name,
[[email protected] ~]# pgrep -u apache -l 4353 httpd 4354 httpd 4355 httpd 4356 httpd 4357 httpd 4358 httpd 4359 httpd 4360 httpd [[email protected] ~]# [[email protected] ~]# pgrep -u apache -a 4353 /usr/sbin/httpd -DFOREGROUND 4354 /usr/sbin/httpd -DFOREGROUND 4355 /usr/sbin/httpd -DFOREGROUND 4356 /usr/sbin/httpd -DFOREGROUND 4357 /usr/sbin/httpd -DFOREGROUND 4358 /usr/sbin/httpd -DFOREGROUND 4359 /usr/sbin/httpd -DFOREGROUND 4360 /usr/sbin/httpd -DFOREGROUND [[email protected] ~]#
Example 5) List the process ids associated to a user using custom delimiter (-d)
Use ‘-d’ option in pgrep to list process ids of user by using custom delimiter, let’s suppose we list the process ids associated to apache user separated by comma (,) or space.
[[email protected] ~]# pgrep -u apache -d, 4353,4354,4355,4356,4357,4358,4359,4360 [[email protected] ~]# pgrep -u apache -d" " 4353 4354 4355 4356 4357 4358 4359 4360 [[email protected] ~]#
Example:6) List newest process name and it’s id (-n)
Let’s assume we want to list newest process name and its ids owned by root user, execute below pgrep command
# pgrep -u <user> …