0
0
Linux-cliHow-ToBeginner · 3 min read

How to Find a Process by Name in Linux Quickly

To find a process by name in Linux, use the pgrep command followed by the process name, like pgrep firefox. Alternatively, use ps aux | grep process_name to list details of matching processes.
📐

Syntax

Here are common commands to find a process by name:

  • pgrep process_name: Lists process IDs matching the name.
  • pidof process_name: Shows the process ID(s) of the named program.
  • ps aux | grep process_name: Lists detailed info of processes matching the name.

Each command helps you identify running processes by their names.

bash
pgrep process_name
pidof process_name
ps aux | grep process_name
💻

Example

This example shows how to find the process ID of the sshd service using pgrep and ps commands.

bash
pgrep sshd
ps aux | grep sshd
Output
1234 root 1234 0.0 0.1 123456 2345 ? Ss 10:00 0:00 /usr/sbin/sshd -D user 5678 0.0 0.0 23456 789 pts/0 S+ 10:05 0:00 grep --color=auto sshd
⚠️

Common Pitfalls

Common mistakes when searching for processes by name include:

  • Using grep without excluding the grep process itself, which shows the grep command in results.
  • Not using exact process names, causing multiple unrelated matches.
  • Running commands without proper permissions, missing some processes.

Use pgrep for cleaner results and add | grep -v grep when using ps aux | grep.

bash
ps aux | grep sshd
ps aux | grep sshd | grep -v grep
Output
user 5678 0.0 0.0 23456 789 pts/0 S+ 10:05 0:00 grep --color=auto sshd root 1234 0.0 0.1 123456 2345 ? Ss 10:00 0:00 /usr/sbin/sshd -D
📊

Quick Reference

CommandDescriptionExample
pgrep process_nameLists process IDs matching the namepgrep sshd
pidof process_nameShows process IDs of the named programpidof sshd
ps aux | grep process_nameLists detailed info of matching processesps aux | grep sshd
ps aux | grep process_name | grep -v grepExcludes the grep command itself from resultsps aux | grep sshd | grep -v grep

Key Takeaways

Use pgrep for a simple and clean way to find process IDs by name.
The ps aux | grep process_name command shows detailed info but may include the grep process itself.
Add | grep -v grep to exclude the grep command from results when using ps and grep.
Use exact process names to avoid unrelated matches.
Run commands with appropriate permissions to see all processes.