ps Command

The ps command (process status) displays information about running processes in Linux and Unix systems. It's essential for system monitoring, troubleshooting, and process management, showing details like process IDs, CPU usage, memory consumption, and process states.

Syntax

ps [options]

Description

The ps command provides a snapshot of currently running processes. It can display processes for the current user, all users, or specific criteria. The output includes process identification numbers (PIDs), resource usage, and process states.

Key features:

  • Display running processes and their details
  • Show process relationships (parent-child)
  • Monitor CPU and memory usage
  • Filter processes by user, terminal, or criteria
  • Sort processes by various attributes

Process Information Fields

  • PID: Process ID (unique identifier)
  • PPID: Parent Process ID
  • USER: Process owner
  • %CPU: CPU usage percentage
  • %MEM: Memory usage percentage
  • VSZ: Virtual memory size
  • RSS: Resident set size (physical memory)
  • TTY: Terminal associated with process
  • STAT: Process state
  • START: Process start time
  • TIME: CPU time used
  • COMMAND: Command that started the process

Common Options

Option Description Example
aux Show all processes with detailed info ps aux
-ef Show all processes (alternative format) ps -ef
-u user Show processes for specific user ps -u john
-p PID Show specific process by PID ps -p 1234
-C command Show processes by command name ps -C firefox
f Show process tree (forest view) ps auxf
--sort Sort by specified field ps aux --sort=-%cpu

Process States (STAT column)

  • R: Running or runnable
  • S: Sleeping (waiting for event)
  • D: Uninterruptible sleep (usually I/O)
  • T: Stopped (by signal or debugger)
  • Z: Zombie (terminated but not reaped)
  • +: Foreground process group
  • <: High priority process
  • N: Low priority process
  • s: Session leader

Examples

Basic process listing

ps # Show processes in current terminal
ps aux # Show all processes with detailed info
ps -ef # Show all processes (alternative format)

Display running processes with different levels of detail

Find specific processes

ps aux | grep firefox # Find Firefox processes
ps -C apache2 # Show Apache processes
ps -u www-data # Show processes owned by www-data
pgrep -l firefox # Alternative: find by name

Search for processes by name, command, or user

Sort and filter processes

ps aux --sort=-%cpu # Sort by CPU usage (highest first)
ps aux --sort=-%mem # Sort by memory usage
ps aux | head -10 # Show top 10 processes
ps aux --no-headers | wc -l # Count total processes

Organize process output for analysis and monitoring

Process tree and relationships

ps auxf # Show process tree (forest view)
ps -ejH # Show process hierarchy
pstree # Alternative tree view
ps -p 1234 -o pid,ppid,cmd # Show parent-child relationship

Visualize process relationships and hierarchies

Custom output format

ps -eo pid,user,cpu,mem,cmd # Custom columns
ps aux --cols=120 # Set output width
ps -o pid,comm,etime # Show PID, command, elapsed time
ps -eo pid,ppid,user,args --forest # Tree with custom fields

Customize ps output to show specific information

Real-time monitoring

watch -n 1 'ps aux --sort=-%cpu | head -10' # Update every second
ps aux | grep -v grep | grep myapp # Monitor specific app
while true; do ps aux | grep firefox; sleep 5; done # Continuous monitoring

Monitor processes continuously for troubleshooting

Sample ps aux Output

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 225868  9876 ?        Ss   Jan20   0:02 /sbin/init
root         2  0.0  0.0      0     0 ?        S    Jan20   0:00 [kthreadd]
john      1234  2.5  5.2 2847364 423876 ?      Sl   09:15   1:23 /usr/bin/firefox
www-data  5678  0.1  0.8 123456  65432 ?       S    10:30   0:05 /usr/sbin/apache2

💡 Tips and Best Practices

  • Use ps aux: Most comprehensive view for general monitoring
  • Combine with grep: Filter output to find specific processes
  • Sort by resource usage: Identify resource-hungry processes
  • Check process states: Look for zombie (Z) or uninterruptible (D) processes
  • Use pgrep/pkill: Simpler alternatives for finding/killing processes
  • Monitor regularly: Use with watch or in scripts for continuous monitoring

Common Use Cases

  • System monitoring: ps aux --sort=-%cpu | head -10
  • Find process to kill: ps aux | grep process_name
  • Check service status: ps -C apache2
  • Monitor user activity: ps -u username
  • Debug hanging processes: ps auxf | grep -A5 -B5 process
  • Resource analysis: ps -eo pid,user,cpu,mem,cmd --sort=-%mem

See also