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
-d
: Set update delay interval (in seconds)-n
: Specify number of iterations-p
: Monitor specific process IDs-u
: Monitor processes owned by a specific user-o
: Sort by a specific column
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
- Interactive, user-friendly interface
- Customizable columns and sorting
- Process tree view
- Support for mouse interaction
- Advanced filtering and search
Usage Examples
# Launch htop
$ htop
# Sort by memory usage
$ htop -s VIRT
# Filter processes by name
$ htop -u myuser
Common Use Cases
-
Performance Monitoring
# Identify resource-intensive processes $ top -o %CPU $ htop
-
Process Management
# Kill a specific process $ kill -9 $(top -n1 -o %CPU | grep -E '^\s*[0-9]+' | head -1 | awk '{print $1}')
-
System Troubleshooting
# Monitor system activity in real-time $ top -d 1 $ htop
Tips and Tricks
-
Customizing top
# Change sort order $ top -o %MEM # Specify columns to display $ top -o %CPU,%MEM,command
-
Automating top
# Run top for a specific number of iterations $ top -n 10 -b > top_output.txt
-
Navigating htop
# Sort by a specific column $ htop -s PERCENT_CPU # Search for a process $ htop -s command -f nginx
Best Practices
-
Monitor Critical Processes
# Watch for high CPU/memory usage $ top -d 5 -o %CPU,%MEM
-
Automate Process Management
# Restart a service if it crashes $ while true; do htop | grep -q nginx || /etc/init.d/nginx restart sleep 60 done
-
Secure Process Information
# Restrict access to process details $ sudo chmod 750 /proc
Common Errors and Solutions
-
Permission Denied
# Use sudo for restricted processes $ sudo top $ sudo htop
-
Process Not Found
# Check if process is still running $ ps -p 1234
-
Incomplete Information
# Use more detailed options $ top -o %CPU,%MEM,command
Related Commands
ps
: Process statuskill
: Terminate processespgrep
: Find processes by namepkill
: Kill processes by namestrace
: Trace system calls
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.