Finding PID in Linux: A Complete Beginner’s Guide

Every program running in Linux gets a unique number called a PID (Process ID). This identifier is the key to managing processes: you can stop them, change their priority, and more. Knowing how to quickly find a PID is one of the basic skills for any Linux user. Let’s go through the simplest and most effective tools for this task.

1. pgrep — the simplest and fastest way

pgrep was made specifically for finding PIDs by process name.


How to use it:

Just type pgrep followed by the process name. For example, to find Firefox:

> pgrep firefox

If several Firefox instances are running, you’ll see multiple PIDs, each on a new line.


Useful options:

  • -l — show PID with process name:
    > pgrep -l fire
    
  • -a — show full command line:
    > pgrep -a fire
    
  • -u — search by user:
    > pgrep -u root
    > pgrep -u root ssh
    
  • -x — exact match (find nginx but not nginx-proxy):
    > pgrep -x nginx
    
  • -f — search by full command, including arguments:
    > pgrep -f "my_script.py"
    
  • -o — find the oldest process.
  • -n — find the newest process.

Pros: Perfect for scripts and quick searches. Faster and cleaner than ps | grep.

Cons: No extra info like CPU or memory usage.



2. pidof — for exact process names

pidof is even simpler. It only finds processes by their exact name.

Examples:

> pidof firefox          # find all PIDs of firefox
> pidof -s firefox       # show only one PID (the first found)

Unlike pgrep, pidof won’t match partial names (fire won’t find firefox).

Pros: Outputs multiple PIDs separated by spaces.

Cons: Less flexible than pgrep (no user filters, etc.).



3. ps — the classic method

ps (process status) is a powerful tool to list all running processes. It shows much more than just PIDs, but you can filter it.


Typical usage with grep:

> ps aux | grep firefox

Explanation:

  • a — show processes of all users.
  • u — show the user who started the process.
  • x — show processes not attached to a terminal (like daemons).

The pipe  |  passes the output of ps into grep.

grep firefox filters only lines containing “firefox


⚠️ Gotcha: you’ll see two lines — the actual Firefox process and the grep firefox command itself.

Fix with square brackets trick:

> ps aux | grep [f]irefox

Now only the real process is shown.

Pros: Works everywhere, no need to install extra tools.

Cons: Output is verbose; requires hacks to clean up grep results.



4. top and htop — interactive monitoring

top is like a dashboard for processes. It shows PID, CPU, memory usage in real time.

Run:

> top

Inside top:

  • Press k and enter PID to kill a process.
  • Press L to search by process name.
  • Press M to sort by memory.
  • Press P to sort by CPU.

htop is an improved version (install with sudo apt install htop on Ubuntu). It has colors, mouse support, and easier navigation.

Pros: Great for live monitoring and sorting.

Cons: Not script-friendly, more for manual use.



5. More advanced methods



🔹 systemctl

If the process is managed by systemd (like nginx, mysql, etc.):

> systemctl status nginx

This will show the PID along with a lot of useful information.



🔹 lsof

A very powerful tool. If you know which port the process is using:

> sudo lsof -i :8080

This will display the PID of the process holding port 8080.

Useful when you get the error “port already in use” but don’t know which process is responsible.



🔹 fuser — find the process using a file

> fuser /var/log/syslog

This will show the PID of the process that is currently using this file.



🔹 /proc filesystem

Every process in Linux has its own directory in /proc/<PID>.

For example, to find a PID by process name:

> grep -i firefox /proc/*/comm

⚠️ Not the most convenient method, but it works even without ps or pgrep.