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.

Prerequisites

  • A Linux operating system (screenshots are from a centos / rhel machine)

pgrep command

Syntax: # pgrep [options] pattern

pgrep-command-options-linux

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 (,)

Output:

pgrep-different-users

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

Syntax:

# pgrep -u <user> -n -l

[[email protected] ~]# pgrep -u root -n -l
11255 kworker/0:2
[[email protected] ~]#

Example:7) List oldest process name and its id (-o)

Let’s suppose we want to list oldest process name and it’s ids owned by root user, run below pgrep command

Syntax:

# pgrep -u <user> -o -l

[[email protected] ~]# pgrep -u root -o -l
1 systemd
[[email protected] ~]#

Example:8) Search Process with its name

Let’s suppose we want list all process ids associated to postgres process

[[email protected] ~]# pgrep postgres -l
1233 postgres
1234 postgres
4465 postgres
4469 postgres
[[email protected] ~]#

And that’s how we use pgrep. Let’s move to pkill command

pkill command

The pkill command is used to kill processes through the command line. As opposed to the kill command, pkill doesn’t need you to put the ID of the process you’re trying to terminate.

So how do you use pkill? Simple, type pkill with the process name that you want to kill as input.

Syntax:

# pkill [options] <pattern>

pkill-command-options-linux

Example:9) Kill process by its name (pkill {process_name})

For instance, if you would like to kill the postgres process, you simply type;

[[email protected] ~]# pkill postgres
[[email protected] ~]#

If the postgres process is running, then it will be terminated instantly.

Example:10) Kill process by sending different signal (–signal)

By default pkill command use “SIGTERM” signal during its execution. We can change this default signal using “–signal” option in pkill command, for instance,

[[email protected] ~]# pkill --signal SIGKILL postgres
[[email protected] ~]#

Example:11) kill process based on the full command (-f)

We can instruct the pkill command to kill a process based on full command instead of process name using the -f option. What does this mean? Let’s say two ping commands are running on your system.

ping google.com

ping linuxtechi.com

Now, if the requirement is only to terminate the ‘ping google.com’ command using pkill. You can use pkill the following way:

[[email protected] ~]# pkill -f "ping google.com"
[[email protected] ~]#

Notice the -f option is used. If you had used;

[[email protected] ~]# pkill ping

then both the commands will be terminated.

Final Thoughts

In this guide, you have learned all about the commands pgrep, and pkill using practical examples.

In summary, pgrep is a command used while searching for processes that are running on the system at the time of use. Its syntax is pgrep [options] pattern.

On the other hand, pkill is used to kill processes through the command line. Its syntax is pkill [options] <pattern>.

You can use the two commands to take advantage of Linux’s ability and flexibility further. Now that you have all the info, what do you think? Please leave a comment, and we’ll be happy to respond.

from Linuxtechi https://www.linuxtechi.com/pgrep-pkill-command-examples-linux/