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.
pgrep
was made specifically for finding PIDs by process name.
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.
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.).
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.
top
is like a dashboard for processes. It shows PID, CPU, memory usage in real time.
Run:
> top
Inside top
:
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.
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.
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 /var/log/syslog
This will show the PID of the process that is currently using this file.
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
.