How to Display Full Process Information Using pgrep Like ps Command


2 views

When working with process management in Linux/Unix systems, many developers prefer pgrep for its simplicity in finding processes by name. However, a common frustration arises when you need more detailed information than just the process ID (PID).

The typical workaround of piping ps through grep has two main drawbacks:

ps aux | grep nginx
  1. It includes the grep process itself in results
  2. It requires more typing and isn't as clean as pgrep's syntax

Here's the most efficient way to get full process information while maintaining pgrep's benefits:

ps -p $(pgrep nginx) -o pid,user,cmd,start,time

This command:

  • Uses pgrep to find all nginx processes
  • Passes the PIDs to ps with the -p flag
  • Specifies exactly which columns to display (-o)

You can tailor the output to show exactly what you need:

# Show full command line and CPU usage
ps -p $(pgrep -d, python) -o pid,ppid,user,%cpu,cmd

# Display in tree format showing parent-child relationships
ps -f --forest -p $(pgrep -d, java)

Combine pgrep's filtering capabilities with ps output:

# Show full info for processes owned by specific user
ps -fp $(pgrep -u www-data -d,)

Remember these useful pgrep flags:

  • -d: Specify delimiter between PIDs (comma for ps -p)
  • -a: Show process name with PID
  • -l: Show both PID and command name

For frequent use, create aliases in your ~/.bashrc:

alias pgrepa='ps -fp $(pgrep -d, $1)'
alias pgrepfull='ps -p $(pgrep -d, $1) -o pid,user,pcpu,pmem,vsz,rss,tty,stat,start,time,cmd'

Now you can simply run:

pgrepa nginx
pgrepfull python

For maximum control, read process info directly from /proc:

pgrep bash | while read pid; do 
    echo "PID: $pid"
    cat /proc/$pid/cmdline
    echo
done

The pgrep command is great for quickly finding process IDs, but by default it only shows PID numbers. Unlike ps which provides comprehensive process information, pgrep lacks detailed output options.

Many developers use this pattern:

ps aux | grep process_name

However, this approach has two key issues:

  • It includes the grep process itself in results
  • Requires more typing than necessary

Here's a more elegant solution that combines both commands:

ps -fp $(pgrep -d, process_name)

Breaking this down:

  • pgrep -d, returns PIDs comma-separated
  • $(...) substitutes the PIDs into ps
  • ps -f shows full format output

For different output formats with ps:

# Show extended format
ps -efl $(pgrep -d, nginx)

# Custom columns
ps -o pid,user,cmd $(pgrep -d, python)

For frequent use, add these to your shell rc file:

alias pfull='ps -fp $(pgrep -d, $1)'
alias pwide='ps -fwwp $(pgrep -d, $1)'

When dealing with multiple matches, consider:

pgrep -d" " process_name | xargs ps -fp

This properly handles spaces between PIDs.