Using top and htop

top and htop display real-time process information with auto-refreshing output. They show CPU usage, memory, load average, and per-process statistics in a continuously updated view. top is always available on any Ubuntu system; htop is a more user-friendly alternative that must be installed but offers color, mouse support, and easier process management.

top: the built-in monitor

# Start top (updates every 3 seconds by default)
top

# Start with a specific user's processes
top -u mysql

# Start sorted by memory instead of CPU
top -o %MEM

# Run for a specified number of iterations and exit
top -b -n 3    # Batch mode, 3 iterations — useful for scripting

Understanding the top header

top header — every field matters

top - 14:22:33 up 5 days,  2:10,  2 users,  load average: 0.85, 0.72, 0.68
Tasks: 187 total,   2 running, 184 sleeping,   0 stopped,   1 zombie
%Cpu(s):  5.2 us,  1.0 sy,  0.0 ni, 92.5 id,  1.0 wa,  0.0 hi,  0.3 si
MiB Mem :  15855.6 total,   4282.5 free,   5320.1 used,   6253.0 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   9812.3 avail Mem
FieldMeaningConcerning when
load average1m/5m/15m avg processes in run queue> number of CPU cores
wa (iowait)CPU time waiting for I/O> 10% sustained
us (user)CPU time in user processes> 80% means application is the bottleneck
sy (system)CPU time in kernel> 20% sustained is unusual
id (idle)CPU doing nothing< 5% means CPU is saturated
buff/cacheDisk cache; Linux reclaims this for processesNot a concern

top interactive commands

KeyAction
1Toggle between aggregate CPU and per-CPU display
MSort by memory usage
PSort by CPU usage (default)
TSort by cumulative CPU time
uFilter by username (enter username when prompted)
kKill a process (enter PID)
rRenice a process (change priority)
cToggle between command name and full command line
HShow threads instead of processes
eCycle memory display units (KB, MB, GB)
qQuit

htop: the enhanced version

# Install htop
sudo apt install -y htop

# Start htop
htop

# Start filtered for a user
htop -u mysql

# Start in tree view (shows process hierarchy)
htop -t

htop advantages over top:

  • Color-coded CPU, memory, swap bars for each core
  • Mouse support: click column headers to sort, click processes to select
  • Tree view: shows parent-child relationships
  • F-key shortcuts prominently displayed at bottom
  • F9 to kill selected process
  • F5 to toggle tree view
  • Space to mark multiple processes, then kill all at once

Non-interactive monitoring

# Capture top output to a file (batch mode)
top -b -n 1 > /tmp/top-snapshot.txt

# Log top output every 30 seconds
while true; do
    top -b -n 1 | head -20 >> /var/log/top-monitor.log
    sleep 30
done

# One-line CPU usage summary from top
top -b -n 1 | grep "Cpu(s)"

# Watch specific processes
watch -n 2 "ps aux | grep mysqld | grep -v grep"

Conclusion

Use top for a quick real-time overview when htop is not installed. Press 1 to see per-CPU usage, M to sort by memory, and c to see full command paths. Install htop for daily use — it is more readable, supports mouse interaction, and the tree view makes process relationships immediately visible. On a server where everything looks fine in top but something is slow, check the wa (iowait) percentage and the load average relative to the number of CPU cores.

FAQ

Is Using top and htop important for Ubuntu administrators?+

Yes. It supports practical Ubuntu administration because it connects directly to server reliability, security, troubleshooting, or daily operations.

Should I practice this on a live server?+

Use a lab VM first. After you understand the command output and rollback path, apply the workflow carefully on real systems.

What should I do after reading this article?+

Run the practice commands, write down what each one shows, and continue to the next article in the Ubuntu roadmap.

Need help with Ubuntu administration?

Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.

Hire Me for Support