top Command
The top command provides real-time monitoring of running processes and system performance in Linux and Unix systems. It displays a continuously updated list of processes sorted by CPU usage, along with system summary information including load averages, CPU usage, and memory statistics.
Syntax
Description
The top command is an interactive system monitor that provides a dynamic real-time view of running processes. It shows system summary information at the top and a list of processes below, updating every few seconds. Top is essential for system administration, performance monitoring, and troubleshooting.
Key features:
- Real-time process monitoring with automatic updates
- System load, CPU, and memory usage statistics
- Interactive commands for sorting and filtering
- Process management (kill, renice) from within top
- Customizable display fields and colors
System Summary Information
- Load Average: 1, 5, and 15-minute system load averages
- Tasks: Total, running, sleeping, stopped, and zombie processes
- CPU Usage: User, system, nice, idle, wait, hardware/software interrupts
- Memory: Total, free, used, and buffer/cache memory
- Swap: Total, free, used, and available swap space
Understanding Load Average
Load average represents system load over time:
- 1.00: System is fully utilized (100% on single-core)
- < 1.00: System has spare capacity
- > 1.00: System is overloaded (processes waiting)
- Multi-core: Load of 4.00 is 100% on quad-core system
Interactive Commands
| Key | Action | Description |
|---|---|---|
q |
Quit | Exit top |
P |
Sort by CPU | Sort processes by CPU usage (default) |
M |
Sort by Memory | Sort processes by memory usage |
T |
Sort by Time | Sort by cumulative CPU time |
k |
Kill Process | Send signal to process (prompts for PID) |
r |
Renice | Change process priority |
u |
Filter User | Show processes for specific user |
1 |
CPU Cores | Toggle individual CPU core display |
Process Display Fields
- PID: Process ID
- USER: Process owner
- PR: Priority
- NI: Nice value
- VIRT: Virtual memory used
- RES: Physical memory used
- SHR: Shared memory
- S: Process status
- %CPU: CPU usage percentage
- %MEM: Memory usage percentage
- TIME+: Total CPU time
- COMMAND: Command name
Sample top Output
top - 14:23:45 up 2 days, 3:15, 2 users, load average: 0.85, 0.92, 1.01 Tasks: 287 total, 1 running, 286 sleeping, 0 stopped, 0 zombie %Cpu(s): 5.2 us, 2.1 sy, 0.0 ni, 92.1 id, 0.6 wa, 0.0 hi, 0.0 si, 0.0 st MiB Mem : 7982.1 total, 1234.5 free, 3456.7 used, 3290.9 buff/cache MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 4123.2 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1234 john 20 0 2847364 423876 89012 S 8.3 5.2 1:23.45 firefox 5678 root 20 0 123456 65432 12345 S 2.1 0.8 0:05.67 apache2
Examples
Basic monitoring
top -d 1 # Update every 1 second
top -n 10 # Run for 10 iterations then exit
top -p 1234,5678 # Monitor specific PIDs only
Start top with different update intervals and monitoring options
User and process filtering
top -u root # Show only root processes
# While in top:
# Press 'u' then enter username to filter
Filter processes by user for focused monitoring
Batch mode and output
top -b -n 1 | head -20 # Show top 20 lines
top -b -n 1 > system_snapshot.txt # Save to file
top -b -d 5 -n 12 > hourly_log.txt # Log for 1 hour
Use batch mode for scripting and logging system performance
Interactive usage workflow
top
# Common interactive commands:
# P - Sort by CPU usage
# M - Sort by memory usage
# k - Kill a process (enter PID)
# u - Filter by user
# 1 - Show individual CPU cores
# q - Quit
Typical workflow for interactive system monitoring
Performance analysis
top -b -n 1 | grep "load average"
# Find memory hogs
top -b -n 1 | head -20 | tail -10
# Monitor specific process
top -p $(pgrep firefox)
Extract specific performance metrics for analysis
💡 Tips and Best Practices
- Use htop for better interface: More user-friendly alternative to top
- Monitor load average: Values > number of CPU cores indicate overload
- Check memory usage: High memory usage can cause swapping
- Sort by different metrics: Use P, M, T keys to find different bottlenecks
- Use batch mode for scripts: Better for automated monitoring
- Filter by user: Focus on specific user's processes
Common Use Cases
- System performance check:
top(quick overview) - Find CPU hogs:
topthen press 'P' - Find memory hogs:
topthen press 'M' - Monitor specific user:
top -u username - System health logging:
top -b -d 60 -n 1440 > daily_log.txt - Quick system snapshot:
top -b -n 1 | head -20