Linux Top Command Usage Tutorial and Tips
1. Basic Usage
Starting top
# Start directly
top
# Set refresh interval (seconds)
top -d 2
# Show only processes of a specific user
top -u username
# Hide idle processes on startup
top -i
2. Overview of the Top Interface
top - 10:30:00 up 10 days, 1:15, 1 user, load average: 0.05, 0.10, 0.15
Tasks: 150 total, 1 running, 149 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.5 us, 0.5 sy, 0.0 ni, 98.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 7952.0 total, 512.0 free, 4096.0 used, 3344.0 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 3500.0 avail Mem
Explanation of Each Area:
- • First Line: System time, uptime, number of users, load average
- • Second Line: Process statistics (total, running, sleeping, stopped, zombie)
- • Third Line: CPU usage statistics
- • Fourth Line: Physical memory usage
- • Fifth Line: Swap usage
3. Interactive Commands (Used While top is Running)
Process Sorting
P - Sort by CPU usage (default)
M - Sort by memory usage
T - Sort by running time
N - Sort by PID
Display Options
l - Toggle load average line display
t - Toggle task/CPU state line display
m - Toggle memory information line display
1 - Expand to show usage for each CPU core
Process Operations
k - Kill process (input PID and signal)
r - Reset process priority (nice value)
Refresh Control
Spacebar - Refresh immediately
s - Change refresh interval (seconds)
4. Practical Tips
Batch Mode (For Scripts)
# Display once and then exit
top -n 1 -b
# Save to file
top -n 1 -b > top_output.txt
# Show only the top 10 processes
top -n 1 -b | head -20
Monitor Specific Processes
# Monitor specific PID
top -p 1234
# Monitor multiple PIDs
top -p 1234,5678,9012
# Monitor processes with names containing nginx
top -p $(pgrep nginx | tr '\n' ',' | sed 's/,$//')
5. Field Explanation and Customization
Display Full Command Line
While running top, press: c
Change Display Fields
f - Enter field management interface
Use up and down keys to select fields, press spacebar to toggle display
d - Select sorting field
Common Field Explanations
- • PID: Process ID
- • USER: Process owner
- • PR: Priority
- • NI: Nice value
- • VIRT: Virtual memory usage
- • RES: Physical memory usage
- • SHR: Shared memory size
- • S: Process state
- • %CPU: CPU usage percentage
- • %MEM: Memory usage percentage
- • TIME+: Cumulative CPU time
6. Advanced Usage
Save Configuration
# After adjusting the display in top
Press W (uppercase) to save configuration to ~/.toprc
Color Display
# Enable color display
top -z
# Switch color during runtime: press Z
Tree View of Processes
# Press: V during runtime
7. Practical Application Examples
Monitor System Performance
# Refresh every 5 seconds, show only important information
top -d 5 -i
# Sort by memory, monitor memory usage
top -o %MEM
Find Processes with Highest Resource Usage
# Sort by CPU
top -o %CPU
# Sort by memory
top -o %MEM
Use in Scripts
#!/bin/bash
# Get the process with the highest CPU usage
HIGH_CPU_PROC=$(top -bn1 -o %CPU | head -8 | tail -1 | awk '{print $12}')
echo "Process with highest CPU usage: $HIGH_CPU_PROC"
# Get memory usage percentage
MEM_USAGE=$(top -bn1 | grep "MiB Mem" | awk '{print $8/$4*100}')
echo "Memory usage: ${MEM_USAGE}%"
8. Recommended Alternative Tools
# htop - Enhanced version of top
htop
# atop - More detailed system monitoring
atop
# nmon - Professional performance monitoring tool
nmon
Summary of Common Shortcuts
- • q – Exit
- • h – Help
- • P/M/T – Sort by CPU/memory/time
- • k – Kill process
- • r – Adjust priority
- • 1 – Show each CPU core
- • z – Toggle color display
Mastering these top command usage tips can help you monitor system performance more effectively and quickly locate problematic processes.