How to Find Process IDs Using the pgrep Command in Linux

When a background application (like a heavy Nginx web server or a rogue Python script) completely locks up on your Linux machine, you must forcefully terminate it. However, you cannot kill a program using its English name; the Linux kernel requires the exact numeric Process ID (PID). The traditional method of finding this number involves running the ps aux command, piping the massive output into grep, and manually searching through hundreds of lines of text. To instantly find the exact PID of any running application in a fraction of a second, you must use the incredibly efficient pgrep command.

How the pgrep Command Works

The pgrep command is a dedicated process-lookup utility designed to completely replace the clunky ps | grep pipeline. It silently scans the entire Linux kernel’s active memory table, searches for the name of the program you requested, and instantly outputs only the raw numeric PID.

If you have an SSH server running in the background and you need to find its Process ID, run:

pgrep sshd

If the server is active, the terminal will instantly output a raw number (e.g., 1405). You can then immediately feed that exact number into the kill command to terminate the service.

Searching for Multiple Processes

If you are running a heavy application like Google Chrome or an Apache web server, the software often spawns dozens of smaller “child” processes to handle individual browser tabs or web requests. If you simply run pgrep apache2, the terminal will dump a massive vertical column of 50 different numbers.

To condense this massive list into a clean, horizontal string separated by spaces (making it incredibly easy to copy and paste), append the -d (delimiter) flag followed by a space enclosed in quotation marks:

pgrep -d " " apache2

The command will output the entire block of process numbers (e.g., 102 104 105 106) perfectly formatted on a single line.

Finding Processes Belonging to Specific Users

On a massive, multi-user enterprise server, multiple engineers might be running instances of the exact same software simultaneously. If you only want to find the instances of a Python script that belong to a specific user (like “john”), you must use the -u flag.

pgrep -u john python3

This command instructs the kernel to completely ignore any Python scripts being run by the root system or other administrators, guaranteeing that it only outputs the specific Process IDs tied directly to John’s account, preventing you from accidentally killing critical system services.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.