Every process running on a Linux system is assigned a unique number called a process ID (PID), and knowing how to get process id in Linux is one of the most practical skills you can develop as a user, developer, or system administrator. Whether you need to stop a runaway application, attach a debugger, monitor resource usage, or write a script that manages background jobs, the PID is the handle that lets you talk to that process directly. This guide walks through every reliable method — from the classic ps command to the /proc filesystem — with clear examples you can copy and run today But it adds up..
What Is a PID in Linux?
A PID is a positive integer that the Linux kernel assigns to each running process. Because of that, the first process started at boot (usually systemd or init) gets PID 1, and every other process receives the next available number. PIDs are recycled once a process exits, so the same number can appear again later — which is why you should always verify a PID before acting on it Less friction, more output..
Honestly, this part trips people up more than it should.
Alongside the PID, Linux tracks a PPID (parent process ID), the process that spawned it. Understanding both helps you trace how an application was launched and which process is responsible for it Worth keeping that in mind. Simple as that..
Why You Need to Know a Process ID
Before diving into commands, it helps to see why PIDs matter in daily work:
- Terminating a stuck process with
kill <PID>instead of guessing. - Monitoring a specific service with
top -p <PID>orps -p <PID>. - Debugging by attaching tools like
straceorgdbto a known PID. - Automating tasks in shell scripts that need to track background jobs.
- Auditing security, such as identifying which process is listening on a port.
Method 1: Using the ps Command
The ps command is the most traditional way to get process IDs. It reads a snapshot of current processes.
ps -ef
This lists every process with columns for UID, PID, PPID, C, STIME, TTY, TIME, and CMD. To find a specific program, combine it with grep:
ps -ef | grep nginx
A cleaner approach uses pgrep-style filtering built into ps:
ps -C nginx -o pid,cmd
The -C flag matches by executable name, and -o pid,cmd prints only the columns you care about. You can also query a PID you already know:
ps -p 1234 -o pid,ppid,user,cmd
Method 2: pgrep — The Precision Tool
pgrep searches the process table by name and returns only PIDs, which makes it perfect for scripting.
pgrep nginx
To see the full command line alongside each PID, add -a:
pgrep -a nginx
Useful variations include:
pgrep -u alice— all processes owned by useralice.pgrep -f "python manage.py"— match against the full command line, not just the process name.pgrep -n nginx— return only the newest matching process.pgrep -o nginx— return only the oldest matching process.pgrep -c nginx— count how many matches exist.
The -f flag is especially handy when a script or interpreter launches your program, because the process name may be python3 or node rather than your application That alone is useful..
Method 3: pidof for Exact Executable Matches
pidof returns the PIDs of a running program by its exact executable name:
pidof firefox
It can print multiple PIDs on one line if several instances are running. Unlike pgrep, pidof does not match partial names, so it is stricter — and often faster for simple lookups. Add -s to get only a single PID:
pidof -s sshd
Method 4: Interactive Monitoring with top and htop
Both top and htop display PIDs in real time.
- In
top, the PID column is shown by default. PressShift+Pto sort by CPU usage, or typepand enter a PID to filter. - In
htop, PIDs appear in the leftmost column. PressF4to filter by name, orF3to search.
For a one-shot snapshot focused on a single process:
top -p 1234 -b -n 1
Method 5: Reading the /proc Filesystem
Linux exposes every running process as a directory under /proc, named after its PID. This is the kernel's own source of truth It's one of those things that adds up..
ls /proc | grep -E '^[0-9]+