16.2 top and htop: Interactive Process Viewers
Right, let’s talk about getting a real-time look under the hood of your system. ps is a fantastic snapshot tool, but when your CPU is screaming and your fans sound like a jet engine, you need a live dashboard, not a photograph. That’s where top and its far superior descendant, htop, come in. They’re our interactive process viewers, and they’re about to become your best friends for diagnosing why your machine is suddenly so interested in mining bitcoin (it’s probably a runaway Chrome tab, not an actual cryptominer, but we’ll find out).
The Quirky Granddad: top
First, the old reliable: top. It’s installed on virtually every Unix-like system by default, which is its main claim to fame. You run it by simply typing top and hitting enter. Boom, you’re in. The interface is, frankly, a relic. It’s a confusing mess of information if you’re not used to it, but let’s break down the crucial parts.
The header shows you the big picture: system uptime, load average (the 1, 5, and 15-minute averages, where a number higher than your CPU core count means trouble), and a summary of CPU time spent on us (user processes), sy (system kernel processes), ni (nice, low-priority processes), and perhaps most importantly, wa (time waiting for I/O – a high number here often means your disk is the bottleneck). Then you get memory info. The real kicker is the process list itself, which by default sorts by CPU usage, so the pesky resource hog is right at the top.
You can’t just click on things; you have to use keyboard commands, which feels archaic but is powerful. Need to kill a process? Press k, then enter the PID. Want to change the sorting order? Press M to sort by memory usage (P to go back to CPU). z toggles colors, which is a godsend for readability. q quits. The problem with top is that its default configuration is… unhelpful. It often shows a cryptic “COMMAND” name instead of the full command line, leaving you guessing what /usr/lib/someobscurebinary actually is.
The Glorious Heir: htop
If top is a quirky granddad who tells stories about punch cards, htop is the cool, competent niece who redesigned his workshop for efficiency. It’s what top should have evolved into. If it’s not installed on your system, rectify that immediately. On Debian/Ubuntu: sudo apt install htop. On Fedora/RHEL: sudo dnf install htop.
The moment you run htop, you’ll see the difference. Color! A bar graph for CPU and memory! A full command line! It’s glorious. The real magic, though, is in the interactivity. You can use the arrow keys to navigate. Want to kill a process? Scroll to it with the arrows and press F9. A nice menu pops up asking which signal you want to send (start with SIGTERM (15), escalate to SIGKILL (9) if it’s being stubborn). Want to re-nice a process (change its priority)? Scroll to it and press F7 or F8. It’s intuitive.
You can search for processes with F3, filter them with F4, and my personal favorite, tree view. Hitting F5 reveals the process hierarchy, showing you exactly which process spawned which others. This is invaluable for understanding how services and their child processes relate.
# Let's say you start a background process
sleep 1000 &
# Now run htop and hit F5. You'll see the sleep command nicely nested under your shell (probably bash or zsh).
# This makes it trivial to see, for example, all the children of a crashed web server that are still hanging around.
Why You Care About Signals (And Which Ones to Use)
When you kill a process in htop (F9), you’re not just “killing” it; you’re sending it a signal. This is a fundamental Unix concept. The polite way to ask a process to shut down is SIGTERM (signal 15). It’s like saying, “Could you please wrap up what you’re doing and close?” The process can catch this signal, perform cleanup operations (like closing files and network connections gracefully), and then exit.
If the process ignores your polite request (often because it’s stuck in a loop or waiting on a unresponsive resource), you bring out the hammer: SIGKILL (signal 9). This signal cannot be caught, blocked, or ignored. The kernel immediately terminates the process and cleans up its resources. It’s effective, but it’s the equivalent of cutting power to the machine—no graceful shutdown occurs. Use it as a last resort, as it can lead to data corruption for applications that aren’t designed for such a brutal end.
Best Practices and Pitfalls
Don’t Panic Kill: See a process using 100% of a CPU core? Don’t immediately
SIGKILLit. It might be doing exactly what it’s supposed to do, like compiling code or rendering video. Investigate first. Usehtop’sF2Setup menu to add a column for the process’s current working directory (CWD) to see where it’s running from.The Load Average Decoder Ring: A load average of 1.0 on a single-core machine means it’s perfectly saturated. On a 4-core machine, it can handle up to ~4.0 before feeling sluggish. If you see load averages of 10.0 on a 2-core machine, you have processes desperately queuing up for CPU time.
Memory Misconceptions: In
htop, the memory bars are great, but remember: Linux uses available memory for disk caching (the “buff/cache” you see). Don’t look at the “Mem” bar and panic if it’s mostly full; look at the “Avail” line below it. Linux is being efficient, not wasteful.The Zombie Apocalypse: You might occasionally see a process marked as
Z(for Zombie) in the status column. This is a process that has finished execution but is still waiting for its parent process to acknowledge its death. A few zombies are usually harmless, but a horde of them can indicate a buggy program that isn’t reaping its children properly. The parent process’s PID is listed in thePPIDcolumn; often, killing the parent will clear them out.