Skip to content

Linux ps Command: Monitoring Processes and System Activity

Published: at 03:43 PMSuggest Changes

The ps (process status) command is a fundamental tool for monitoring and managing running processes in a Linux system. It provides detailed information about the current state of processes, making it an essential utility for system administrators and DevOps professionals.

Basic Syntax

ps [options]

Common Options

Real-World Examples

1. View All Running Processes

# Show all running processes
$ ps -ef

# Show processes in a tree-like format
$ ps -efH

2. Monitor Specific Processes

# Show details for a specific process
$ ps -l -p 1234

# View processes owned by a user
$ ps -u username

3. Filter Processes

# Show processes containing "nginx" in the command
$ ps aux | grep nginx

# List processes by CPU or memory usage
$ ps -eo pid,user,%cpu,%mem,command --sort=-%cpu | head

Common Use Cases

  1. Troubleshooting

    # Identify resource-intensive processes
    $ ps -eo pid,user,%cpu,%mem,command --sort=-%cpu
    
    # Find processes that have crashed or hung
    $ ps -ef | grep -v grep | grep -i defunct
    
  2. Process Monitoring

    # Monitor processes in real-time
    $ watch -n 1 "ps -eo pid,user,%cpu,%mem,command --sort=-%cpu"
    
    # View process hierarchy
    $ ps -efH
    
  3. Process Management

    # Kill a specific process
    $ kill -9 1234
    
    # Terminate all processes owned by a user
    $ pkill -u username
    

Tips and Tricks

  1. Customize Output

    # Show only specific columns
    $ ps -eo pid,user,%cpu,%mem,command
    
    # Sort by a specific column
    $ ps -eo pid,user,%cpu,%mem,command --sort=-%cpu
    
  2. Use with Other Commands

    # Find processes using a lot of memory
    $ ps -eo pid,user,%mem,command --sort=-%mem | head
    
    # List open files for a process
    $ lsof -p 1234
    
  3. Scripting with ps

    # Kill processes older than 1 hour
    $ ps -eo pid,etime | awk '$2 > "01:00:00" { print $1 }' | xargs kill -9
    

Best Practices

  1. Monitor Critical Processes

    # Monitor system services
    $ ps -ef | grep systemd
    
    # Check for zombie processes
    $ ps -eo stat,ppid,pid,comm | grep -e '^[Zz]'
    
  2. Automate Process Management

    # Restart a service if it crashes
    $ while true; do
        ps -C nginx || /etc/init.d/nginx restart
        sleep 60
    done
    
  3. Secure Process Information

    # Restrict access to process details
    $ sudo chmod 750 /proc
    

Common Errors and Solutions

  1. Permission Denied

    # Use sudo for restricted processes
    $ sudo ps -ef
    
  2. Process Not Found

    # Check if process is still running
    $ ps -p 1234
    
  3. Incomplete Information

    # Use more detailed options
    $ ps -eo pid,user,%cpu,%mem,command
    

Advanced Usage

1. Scripting with ps

#!/bin/bash
# Kill processes using excessive CPU
for pid in $(ps -eo pid,%cpu --sort=-%cpu | awk '/[0-9]+/ {if ($2 > 50) print $1}'); do
    echo "Killing process $pid"
    kill -9 $pid
done

2. Monitoring Specific Users

#!/bin/bash
# Monitor processes for a specific user
user="myuser"
while true; do
    echo "Processes for $user:"
    ps -u "$user" -o pid,user,%cpu,%mem,command
    sleep 60
done

3. Integrating with Other Tools

# Use ps with grep to find Java processes
ps -ef | grep java

# Combine with top to show top resource-intensive processes
ps -eo pid,user,%cpu,%mem,command --sort=-%cpu | head

The ps command is a powerful tool for monitoring and managing processes in a Linux system. Understanding its usage and options is crucial for system administrators and DevOps professionals to effectively troubleshoot, optimize, and automate their environments.


Previous Post
Linux top and htop Commands: Real-Time Process Monitoring
Next Post
Linux umask Command: Setting Default File Permissions