If you need to manage processes on your system, knowing how to find pid in linux is a fundamental skill. A process ID in Linux is obtained using the ps aux command and searching for the process name. This simple method gives you the numeric identifier you need to control or monitor any running program.
Every running program on Linux gets a unique number called a Process ID (PID). You use PIDs to stop, restart, or check the status of processes. Without knowing the PID, you cannot send signals to a process or kill it properly.
This guide covers multiple ways to locate PIDs. You will learn command-line tools, scripting methods, and GUI approaches. Each method works for different situations, from quick checks to automation tasks.
How To Find Pid In Linux
The ps command is the most common tool for listing processes. It shows a snapshot of current processes with their PIDs, CPU usage, memory usage, and command names.
To see all processes running on your system, type:
ps aux
This outputs a long list. You can scroll through it to find the process you need. The second column shows the PID.
For a faster search, pipe the output to grep:
ps aux | grep firefox
This filters the list to show only lines containing “firefox”. The first number after the username is the PID.
Be careful: grep itself appears in the output as a separate process. You can ignore that line.
Using Pgrep For Direct PID Lookup
The pgrep command is designed specifically for finding PIDs. It returns only the numeric IDs, making it perfect for scripting.
Basic usage:
pgrep firefox
This prints the PID of any process named “firefox”. If multiple instances run, it shows all PIDs on separate lines.
You can combine pgrep with options:
-u username– show processes owned by a specific user-x– match the exact process name-l– show both PID and process name
Example:
pgrep -u root -l sshd
This finds all SSH daemon processes owned by root.
Using Pidof For Exact Matches
pidof is another dedicated tool. It finds the PID of a running program by its exact name.
Syntax:
pidof program_name
Example:
pidof bash
This returns the PID of the bash shell. If multiple bash processes run, it shows all PIDs space-separated.
pidof is simpler than pgrep but less flexible. It does not support regex patterns or user filters.
Using Top Or Htop
The top command shows real-time process information. It updates every few seconds by default.
To find a PID with top:
- Run
topin the terminal - Press
/to search - Type the process name and press Enter
- The cursor jumps to the matching line
- The PID is in the first column
htop is an improved version with color and mouse support. Install it if not available:
sudo apt install htop # Debian/Ubuntu
sudo yum install htop # RHEL/CentOS
In htop, you can scroll and search interactively. The PID column is clearly labeled.
Using The /Proc Filesystem
Linux stores process information in the /proc directory. Each running process has a subdirectory named after its PID.
To list all PIDs:
ls /proc | grep -E '^[0-9]+$'
This shows only numeric directories, which are PIDs. You can check the comm file inside each directory to find the process name:
cat /proc/1234/comm
This method is useful for scripting but slower than dedicated commands.
Using Systemd Tools
If your system uses systemd, you can use systemctl to find PIDs of services.
For a specific service:
systemctl show -p MainPID sshd.service
This returns the PID of the SSH daemon. For all PIDs of a service with multiple processes:
systemctl show -p MainPID,ControlPID sshd.service
You can also list all running services and their PIDs:
systemctl list-units --type=service --state=running
This shows service names but not PIDs directly. Use systemctl status for detailed info:
systemctl status sshd.service
The output includes the PID in the “Main PID” line.
Using Lsof To Find PIDs By File Or Port
lsof lists open files and their associated processes. You can find a PID by the file it uses or the network port it listens on.
To find which process uses port 80:
lsof -i :80
This shows the PID in the second column. For a specific file:
lsof /var/log/syslog
This lists all processes that have the file open.
Using Fuser To Find PIDs By File
fuser identifies processes using a file or socket. It is faster than lsof for simple checks.
Example:
fuser /var/log/syslog
This prints the PIDs of processes accessing the file. Add the -v flag for verbose output:
fuser -v /var/log/syslog
Using Grep With Ps For Scripting
In scripts, you often need to extract the PID cleanly. A common pattern is:
pid=$(ps aux | grep "firefox" | grep -v grep | awk '{print $2}')
This stores the PID in a variable. The grep -v grep removes the grep process itself. awk '{print $2}' extracts the second column (PID).
For more reliability, use pgrep instead:
pid=$(pgrep firefox)
Finding PIDs Of Child Processes
Sometimes you need the PID of a parent process or its children. Use pstree to see the hierarchy:
pstree -p
This shows all processes in a tree with PIDs. For a specific process:
pstree -p firefox
You can also use ps with the --ppid option:
ps --ppid 1234
This lists all child processes of PID 1234.
Using Awk And Sed For Advanced Parsing
When ps aux output is complex, you can parse it with awk or sed.
Example: Find PIDs of processes using more than 50% CPU:
ps aux | awk '$3 > 50 {print $2}'
This prints the PID (column 2) when CPU usage (column 3) exceeds 50%.
For memory usage:
ps aux | awk '$4 > 10 {print $2}'
This finds processes using more than 10% memory.
Finding PIDs With Watch Command
If a process runs briefly, use watch to repeatedly check for its PID.
watch -n 1 'pgrep -l myprocess'
This updates every second, showing the PID if the process appears.
Using Python Or Other Languages
For complex scripts, you can use Python to find PIDs:
import os
import subprocess
output = subprocess.check_output(['pgrep', 'firefox'])
pids = output.decode().strip().split('\n')
print(pids)
This returns a list of PIDs. You can also parse /proc directly in Python.
Common Pitfalls And Solutions
One common mistake is confusing the PID with the process name. Always double-check the number before using it.
Another issue: grep matches partial names. Use pgrep -x for exact matches.
If you get “command not found”, install the package. For pgrep, install procps on Debian/Ubuntu or procps-ng on RHEL/CentOS.
Be aware that PIDs are reused after a process ends. A PID you found earlier might belong to a different process now.
Practical Examples
Example 1: Find and kill a frozen Firefox:
- Run
pgrep firefox - Note the PID, e.g., 5678
- Run
kill 5678
Example 2: Find all SSH processes:
ps aux | grep ssh
Example 3: Find PIDs of processes using a specific port:
lsof -i :8080
Example 4: Monitor a process by PID:
top -p 1234
This shows only the process with PID 1234.
Automating PID Discovery
In cron jobs or scripts, use pgrep for reliability. Example script to restart a service if it crashes:
#!/bin/bash
pid=$(pgrep myservice)
if [ -z "$pid" ]; then
systemctl start myservice
echo "Service restarted"
fi
This checks if the PID exists. If not, it starts the service.
Using GUI Tools
If you prefer a graphical interface, use System Monitor (GNOME) or KSysGuard (KDE). These show PIDs in a table format.
To open System Monitor:
- Press Super key and type “System Monitor”
- Click the Processes tab
- Find your process and note the PID column
These tools are good for beginners but slower than command-line methods.
Security Considerations
Finding PIDs is safe, but acting on them requires caution. Only kill processes you own or have permission to manage.
Using sudo kill on system processes can crash your system. Always verify the PID before sending signals.
Some processes hide from ps and pgrep if they are kernel threads. Use ps -e to see all processes, including kernel ones.
Summary Of Commands
| Command | Purpose |
|---|---|
ps aux | grep name |
Find PID by name |
pgrep name |
Direct PID lookup |
pidof name |
Exact match PID |
lsof -i :port |
Find PID by port |
fuser file |
Find PID by file |
top or htop |
Interactive PID view |
Frequently Asked Questions
What is the fastest way to find a PID in Linux?
The fastest method is using pgrep followed by the process name. It returns only numeric PIDs without extra output.
Can I find a PID without using the command line?
Yes, GUI tools like System Monitor or KSysGuard show PIDs in a graphical interface. They are easier for beginners but slower.
Why does ps aux | grep show two lines for one process?
The second line is the grep command itself. Use grep -v grep to filter it out, or use pgrep instead.
How do I find the PID of a background process?
Use jobs -l to see PIDs of background jobs in the current shell. For other background processes, use ps aux or pgrep.
What if pgrep returns multiple PIDs?
That means multiple instances of the process are running. You can kill them all with pkill name or handle each PID individually.
Now you have multiple ways to find PIDs in Linux. Start with ps aux | grep for quick checks, and use pgrep for scripting. Practice these commands to become proficient in process management.