How to Get Process ID in Linux
Getting the process ID (PID) in Linux is a common task you might need to do when managing or troubleshooting your system. Whether you want to monitor a program, kill a process, or check resource usage, knowing how to find the PID is essential. In this article, I’ll guide you through simple and practical methods to get the process ID in Linux.
You don’t need to be a Linux expert to follow along. I’ll explain everything clearly and give you examples you can try right away. By the end, you’ll feel confident using commands like ps, pidof, and pgrep to find any process ID you need.
What Is a Process ID (PID) in Linux?
A process ID, or PID, is a unique number assigned by the Linux kernel to every running process. Think of it as the process’s ID card. It helps the system keep track of each program or task running on your computer.
- Every process has a PID.
- PIDs are assigned sequentially but can wrap around after reaching a limit.
- You use PIDs to manage processes, like stopping or monitoring them.
Knowing the PID is useful when you want to control a process or check its status. For example, if a program freezes, you can find its PID and kill it.
Using the ps Command to Find Process IDs
The ps command is one of the most common ways to see running processes and their PIDs. It shows a snapshot of current processes with details like PID, user, CPU usage, and more.
Here’s how to use it:
- Open your terminal.
- Type
ps auxand press Enter.
This command lists all running processes with their PIDs in the second column.
Example output snippet:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 169084 5644 ? Ss 10:00 0:01 /sbin/init
john 2345 0.1 1.2 256000 24500 pts/0 S+ 10:05 0:00 bash
- The
PIDcolumn shows the process ID. - You can search for a specific process by combining
pswithgrep.
For example, to find the PID of Firefox:
ps aux | grep firefox
This filters the list to show only processes with "firefox" in their name.
Using ps with Process Name Filtering
If you want a cleaner output, use:
ps -C firefox -o pid=
-C firefoxselects processes by command name.-o pid=outputs only the PID without headers.
This command returns just the PID(s) of Firefox processes.
Using the pidof Command for Quick PID Lookup
pidof is a simple command designed specifically to find the PID of a running program by its name.
To use it:
pidof firefox
This will return one or more PIDs for Firefox processes.
Key points about pidof:
- It only works with processes that have a unique executable name.
- If multiple instances run, it returns all their PIDs separated by spaces.
- It’s faster and simpler than
psfor this specific task.
If the process isn’t running, pidof returns nothing.
Using pgrep to Search for Process IDs
pgrep is another powerful tool to find PIDs by matching process names or other attributes.
Basic usage:
pgrep firefox
This lists all PIDs of processes with "firefox" in their name.
Advantages of pgrep:
- Supports pattern matching with regular expressions.
- Can filter by user, group, or other criteria.
- Outputs only PIDs, making it easy to use in scripts.
Example: Find PIDs of processes owned by user "john":
pgrep -u john
You can combine options for precise searches.
Finding PID Using the /proc Filesystem
Linux exposes process information through the /proc directory. Each running process has a folder named by its PID under /proc.
To find a PID by process name using /proc:
- List all directories in
/procthat are numbers (these are PIDs). - Check the
commorcmdlinefile inside each for the process name.
Example command to find PID of "bash":
for pid in /proc/[0-9]*; do
if grep -q "bash" "$pid/comm" 2>/dev/null; then
echo "PID: $(basename $pid)"
fi
done
This loops through all PIDs and prints the ones running bash.
This method is more advanced but useful if you want to explore process details manually.
How to Get PID of a Process You Just Started
Sometimes you start a process in the terminal and want to know its PID immediately.
You can do this by:
- Running the process in the background with
&. - Using the special variable
$!which stores the PID of the last background process.
Example:
firefox &
echo "PID is $!"
This starts Firefox in the background and prints its PID.
This is handy for scripting or when you want to track a process you just launched.
Using top or htop to Find Process IDs Interactively
If you prefer a visual, interactive way to find PIDs, top and htop are great tools.
- Run
topin the terminal. - It shows a live list of processes with PIDs, CPU, memory, and more.
- Use the search function (press
/intop) to find a process by name.
htop is a more user-friendly version of top with color and easier navigation.
- Install it if not available (
sudo apt install htop). - Run
htop. - Use arrow keys and search to find your process.
- The PID is shown in the first column.
These tools are useful for real-time monitoring and quick PID lookup.
How to Use PID to Manage Processes
Once you have the PID, you can do many things:
- Kill a process:
kill PID
- Force kill:
kill -9 PID
- Check process status:
ps -p PID -o pid,cmd,%cpu,%mem
- Trace system calls:
strace -p PID
Knowing the PID lets you control and troubleshoot processes effectively.
Summary Table of Commands to Get PID
| Command | Description | Example | ||
| `ps aux | grep name` | List processes and filter by name | `ps aux | grep firefox` |
ps -C name -o pid= | Get PID(s) by command name | ps -C bash -o pid= | ||
pidof name | Get PID(s) of a running program | pidof sshd | ||
pgrep name | Search for PID(s) by name or pattern | pgrep apache2 | ||
/proc/[pid]/comm | Check process name by PID | cat /proc/1234/comm | ||
top or htop | Interactive process viewer | Run top or htop | ||
$! | PID of last background process | firefox &; echo $! |
Conclusion
Finding the process ID in Linux is easier than you might think. Whether you use the classic ps command, the quick pidof, or the flexible pgrep, you have many options to get the PID you need. For interactive use, tools like top and htop provide a visual way to spot processes and their IDs.
Remember, the PID is your key to managing processes—whether you want to monitor, stop, or debug them. With these commands and tips, you can confidently find and use process IDs in your Linux system.
FAQs
How do I find the PID of a process by name?
Use pidof processname or pgrep processname to quickly find the PID(s) of a running process by its name.
Can I get the PID of a process I just started?
Yes, if you start a process in the background with &, the variable $! holds its PID.
What is the difference between pidof and pgrep?
pidof returns PIDs by exact executable name, while pgrep supports pattern matching and more filtering options.
How do I find the PID of a process using ps?
Run ps aux | grep processname to list processes and filter by name, then check the PID column.
Can I find process IDs without using commands?
Yes, you can explore the /proc directory where each running process has a folder named by its PID. Checking files like comm inside helps identify processes.
