When you are managing a Linux server, you frequently need to check the status of a specific background service, such as an Nginx web server or an SSH daemon, to see if it is running or to find its exact Process ID (PID) so you can kill it. While you could use the ps command and pipe the massive output into grep, this is inefficient and clunky. The cleanest, most precise way to look up running processes based entirely on their name is to use the pgrep command.
How the pgrep Command Works
The pgrep (process grep) utility searches the system’s list of active processes and returns the unique numerical PID of any process whose name matches the search pattern you provide. By default, it outputs nothing but the raw PID numbers, making it incredibly useful for shell scripts that need to capture IDs to pass to other commands (like kill).
Basic Process Lookup
To find the PID of a specific program, simply type pgrep followed by the name of the executable.
pgrep sshd
If the SSH daemon is currently running, the command will output one or more PIDs on separate lines.
912
1455
1480
If the command returns absolutely nothing and returns you straight to the prompt, it means no running process on the system matched your search term.
Displaying the Process Name
Because returning a raw list of numbers can be confusing (especially if a service has spawned dozens of worker processes), you might want visual confirmation that pgrep has actually found the correct software. You can force the command to print the name of the process alongside the PID by adding the -l (list name) flag.
pgrep -l nginx
The output is immediately much easier for a human administrator to read:
1042 nginx
1043 nginx
1044 nginx
Matching the Exact Name
By default, pgrep performs a partial substring match. If you search for ssh, it will return the PIDs for sshd, ssh-agent, and any active ssh client sessions. If you only want to find the exact daemon and nothing else, use the -x (exact match) flag.
pgrep -x sshd
This strict search ensures you do not accidentally capture the PID of a similarly named, but entirely different, application when scripting automated server maintenance tasks.