Skip to content

Linux top and htop Commands: Real-Time Process Monitoring

Published: at 03:44 PMSuggest Changes

The top and htop commands are powerful tools for real-time monitoring and management of running processes in a Linux system. They provide a dynamic, interactive view of system activity, making them essential utilities for DevOps professionals and system administrators.

top Command

Basic Syntax

top [options]

Common Options

Usage Examples

# Display top resource-consuming processes
$ top

# Sort by CPU usage
$ top -o %CPU

# Monitor a specific process
$ top -p 1234

# Run top for 5 iterations
$ top -n 5

htop Command

Basic Syntax

htop

Key Features

Usage Examples

# Launch htop
$ htop

# Sort by memory usage
$ htop -s VIRT

# Filter processes by name
$ htop -u myuser

Common Use Cases

  1. Performance Monitoring

    # Identify resource-intensive processes
    $ top -o %CPU
    $ htop
    
  2. Process Management

    # Kill a specific process
    $ kill -9 $(top -n1 -o %CPU | grep -E '^\s*[0-9]+' | head -1 | awk '{print $1}')
    
  3. System Troubleshooting

    # Monitor system activity in real-time
    $ top -d 1
    $ htop
    

Tips and Tricks

  1. Customizing top

    # Change sort order
    $ top -o %MEM
    
    # Specify columns to display
    $ top -o %CPU,%MEM,command
    
  2. Automating top

    # Run top for a specific number of iterations
    $ top -n 10 -b > top_output.txt
    
  3. Navigating htop

    # Sort by a specific column
    $ htop -s PERCENT_CPU
    
    # Search for a process
    $ htop -s command -f nginx
    

Best Practices

  1. Monitor Critical Processes

    # Watch for high CPU/memory usage
    $ top -d 5 -o %CPU,%MEM
    
  2. Automate Process Management

    # Restart a service if it crashes
    $ while true; do
        htop | grep -q 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 top
    $ sudo htop
    
  2. Process Not Found

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

    # Use more detailed options
    $ top -o %CPU,%MEM,command
    

Advanced Usage

1. Scripting with top

#!/bin/bash
# Kill processes using excessive CPU
while true; do
    top -bn1 | awk '/^ *[0-9]+/ { if ($9 > 50) print $1 }' | xargs kill -9
    sleep 60
done

2. Monitoring Specific Users

#!/bin/bash
# Monitor processes for a specific user
user="myuser"
while true; do
    echo "Processes for $user:"
    top -u "$user"
    sleep 60
done

3. Integrating with Other Tools

# Use top with grep to find Java processes
top | grep java

# Combine with ps to show detailed process information
top -o %CPU,%MEM,command

The top and htop commands are essential tools for real-time process monitoring and management in a Linux environment. Understanding their usage and capabilities is crucial for DevOps professionals to effectively troubleshoot, optimize, and automate their systems.


Previous Post
Linux kill Command: Terminating Processes Effectively
Next Post
Linux ps Command: Monitoring Processes and System Activity