pgrep Command in Linux: Find Processes by Name and Return PID
The pgrep command in Linux allows you to search for expressions in the list of running processes and returns the PID of the process in question. It also allows signals to be sent to those processes listed in the search expression.
In this example, pgrep lists all PIDs of nginx processes:
$ pgrep nginx279912799327994
To list all PIDs of processes owned by root:
$ pgrep –u root123
To send a signal to processes, simply use the --signal option and the signal number. In this example, the SIGHUP signal is sent to nginx:
# pgrep --signal 1 nginx279912799327994
Shows PID of processes whose owner is root and have the name named:
$ pgrep -u root named
Makes the syslog process reread its configuration file:
$ pkill -HUP syslogd
Shows detailed information of all processes connected to the xterm terminal:
$ ps -fp $(pgrep -d, -x xterm)
You can combine the use of pgrep with shell expansions, as in the example that makes Firefox use less CPU:
$ renice +4 $(pgrep firefox)
Difference between pgrep and killall
The pgrep command can read the process list based on the name or a series of attributes, while kill only searches by process name.
pgrep works similarly to pkill.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Did you like it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.