How To Find Pid Of A Process In Linux – Linux Process PID Finding Commands

Locating a process ID in Linux requires running `pgrep` followed by the process name. If you need to know how to find pid of a process in linux, this guide covers every practical method, from simple commands to advanced troubleshooting.

Every running program in Linux gets a unique number called a Process ID (PID). You need it to kill stuck processes, monitor system resources, or debug applications. The good news is that Linux offers multiple ways to find PIDs quickly.

This article walks you through command-line tools like `pgrep`, `pidof`, `ps`, `top`, and `htop`. You’ll also learn how to find PIDs from scripts, handle processes with the same name, and avoid common mistakes.

How To Find Pid Of A Process In Linux

Let’s start with the fastest and most reliable method. The `pgrep` command searches through running processes and returns their PIDs based on name or other attributes.

Open your terminal and type:

pgrep firefox

This returns one or more numbers. Each number is a PID of a Firefox process. If nothing appears, the process isn’t running or you mistyped the name.

To see both the PID and the process name, use the `-l` flag:

pgrep -l firefox

Output example:

1234 firefox
5678 firefox

This confirms which PID belongs to which process. `pgrep` is case-sensitive by default. Use `-i` for case-insensitive matching:

pgrep -i Firefox

Using Pidof For Exact Name Matches

The `pidof` command works similarly but only matches exact process names. It’s simpler and often faster.

pidof firefox

It returns space-separated PIDs. If you need only one PID, combine with `cut`:

pidof firefox | cut -d' ' -f1

This gives you the first PID. `pidof` doesn’t show the process name, so you must already know what you’re looking for.

Using Ps To List All Processes

The `ps` command is the classic way to view process information. It’s more flexible but requires a bit more typing.

To find a PID with `ps`, use:

ps aux | grep firefox

This shows all processes with “firefox” in their details. The second column is the PID. The output includes the user, CPU usage, memory, and more.

For a cleaner view, use `ps` with specific columns:

ps -e -o pid,comm | grep firefox

This prints only the PID and command name. The `-e` flag shows all processes, and `-o` lets you choose output columns.

Understanding Ps Output

The `ps aux` output has these columns: USER, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT, START, TIME, COMMAND. The PID is always the second field.

If you see multiple lines for the same process, each is a thread or child process. The main PID is usually the first one listed.

Using Top And Htop For Interactive Searching

When you want to find PIDs visually, `top` and `htop` are excellent tools. They show real-time process lists with PIDs clearly displayed.

Run `top` in your terminal:

top

The PID column is the first numeric column. Press `q` to quit. To search for a process, press `/` while in `top` and type the name.

`htop` is more user-friendly. Install it if needed:

sudo apt install htop   # Debian/Ubuntu
sudo yum install htop   # RHEL/CentOS

Run `htop` and use the `F3` key to search. PIDs are shown in the leftmost column. Press `F10` to exit.

Finding Pid From A Script Or File

Sometimes you need to capture a PID in a shell script. Use `pgrep` inside command substitution:

pid=$(pgrep -x firefox)
echo "Firefox PID is $pid"

The `-x` flag ensures an exact match. Without it, `pgrep` matches partial names like “firefox-esr” or “firefox-bin”.

For multiple PIDs, store them in an array:

pids=($(pgrep firefox))
echo "First PID: ${pids[0]}"

This works in bash and similar shells.

Handling Processes With The Same Name

Many Linux systems run multiple instances of the same program. For example, you might have several Firefox tabs, each as a separate process.

To find the correct PID, use additional filters. The `pgrep` command can match by user:

pgrep -u username firefox

This returns PIDs only for that user. Combine with `-l` to see names:

pgrep -u username -l firefox

You can also match by parent PID. If you know the main process PID, find its children with:

pgrep -P 1234

This lists all child processes of PID 1234.

Using /Proc Filesystem Directly

Linux stores process information in the `/proc` directory. Each running process has a subdirectory named after its PID.

To list all PIDs, view the directories in `/proc`:

ls /proc | grep -E '^[0-9]+$'

This shows numeric directories only. To find a specific process, read the `comm` file inside each directory:

cat /proc/1234/comm

This returns the process name. For a script, loop through `/proc`:

for pid in /proc/[0-9]*; do
  if [[ $(cat $pid/comm) == "firefox" ]]; then
    echo ${pid#/proc/}
  fi
done

This method is low-level and useful when standard commands are unavailable.

Using Systemd Tools

If your system uses systemd, you can find PIDs of services with `systemctl`:

systemctl show --property MainPID sshd

This returns the main PID of the SSH daemon. For all PIDs of a service, use:

systemctl show --property PIDs sshd

This is handy for services managed by systemd, like web servers or databases.

Finding Pid Of A Process In Linux With Lsof

The `lsof` command lists open files and their associated processes. It can find PIDs by network port or file.

To find which process is using port 80:

lsof -i :80

The second column is the PID. To find a process by file:

lsof /var/log/syslog

This shows all processes with that file open.

Using Pstree For Hierarchical View

The `pstree` command shows processes in a tree format, including PIDs.

pstree -p | grep firefox

This displays the process hierarchy with PIDs in parentheses. It’s useful for understanding parent-child relationships.

To see the full tree for a specific PID:

pstree -p 1234

Common Mistakes And How To Avoid Them

One frequent error is confusing PIDs with thread IDs. Threads often appear as separate processes in `ps` output. Use `ps -T` to see threads.

Another mistake is using `grep` on `ps` output and including the grep process itself. For example:

ps aux | grep firefox

This might show a line like “grep firefox” in the output. To avoid this, use `pgrep` instead, or add `grep -v grep` to your pipeline.

Case sensitivity also trips people up. `pgrep firefox` won’t find “Firefox” or “FIREFOX”. Use `-i` for case-insensitive searches.

Automating Pid Discovery In Scripts

For robust scripts, always check if a PID was found before using it:

pid=$(pgrep -x firefox)
if [[ -n $pid ]]; then
  echo "Found PID: $pid"
else
  echo "Process not found"
fi

This prevents errors when the process isn’t running. For multiple PIDs, iterate over them:

for pid in $(pgrep firefox); do
  echo "Killing $pid"
  kill $pid
done

Always test your scripts with `echo` before running destructive commands like `kill`.

Performance Considerations

On systems with thousands of processes, `pgrep` is faster than looping through `/proc`. The `ps` command can also be slow if you use complex filters.

For frequent PID lookups, consider caching results or using tools like `htop` for interactive work. Avoid running `ps aux` repeatedly in scripts.

Security And Permissions

Regular users can only see their own processes. To see all PIDs, run commands with `sudo`:

sudo pgrep -u root sshd

This shows PIDs of root’s processes. Without `sudo`, you’ll get an empty result for other users’ processes.

Some processes hide their names for security. Use `ps -e` to see all processes, but names may appear as “kworker” or “kthreadd”.

Troubleshooting When Pid Is Not Found

If `pgrep` returns nothing, the process might have a different name. Check with `ps aux` and look at the last column. For example, “chrome” might be “google-chrome” or “chromium-browser”.

Use wildcards with `pgrep`:

pgrep -l chrome

This matches “chrome”, “google-chrome”, “chromium”, etc. To see all running processes, use:

ps -e

This lists every process with its PID and name.

Using Pid In Practice

Once you have the PID, you can kill the process:

kill 1234

Or send a specific signal:

kill -9 1234   # Force kill

You can also check process details:

cat /proc/1234/status

This shows memory, CPU, and state information.

Summary Of Commands

  • pgrep process_name – Fastest method
  • pidof process_name – Exact match
  • ps aux | grep process_name – Detailed info
  • top or htop – Interactive
  • ls /proc – Low-level
  • lsof -i :port – By network port
  • systemctl show --property MainPID service – Systemd services

Each method has its strengths. For daily use, `pgrep` is your best friend. For debugging, `ps aux` gives more context.

Frequently Asked Questions

What Is The Fastest Way To Find A PID In Linux?

The fastest way is using `pgrep` followed by the process name. It searches directly through kernel process tables and returns PIDs instantly.

How Do I Find The PID Of A Process By Port Number?

Use `lsof -i :port_number` or `ss -tlnp | grep port_number`. Both show the PID of the process listening on that port.

Can I Find A PID Without Using Command Line Tools?

Yes, you can check `/proc` directory. Each numbered folder is a PID. Read the `comm` file inside to see the process name.

Why Does Pgrep Return Multiple PIDs For One Program?

Many programs spawn child processes or threads. Each gets its own PID. For example, a web browser often runs one process per tab.

How Do I Find The PID Of A Zombie Process?

Zombie processes appear in `ps aux` with a “Z” in the STAT column. Use `ps aux | grep Z` to find them. Their PID is still visible in `/proc`.

Now you know multiple ways to find PIDs in Linux. Start with `pgrep` for speed, use `ps` for details, and explore `/proc` for deeper understanding. Practice these commands, and you’ll handle processes like a pro.