CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Process Management / htop

htop

Interactive process viewer with bars and a tree

Beginnermonitoringinteractiveprocesses

htop is a friendlier top: per-core CPU bars, a memory bar, scrolling, a process tree, search and filter, and killing or renicing with function keys. It is not always installed on servers, but when it is, it is the fastest way to explore a busy box.

Syntaxhtop [-u USER] [-p PID,...] [-t] [-d TENTHS] [-s COLUMN]

Watch it run

CPU alerts fired on a 4-core box. Get a picture of every core and of memory in one screen.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 1
htop

Each numbered bar is one core: green is user time, red kernel time. The Mem bar shows used (green), buffers (blue) and cache (yellow); 3.87G/7.67G counts only real use. The list shows full command lines, so you can tell the three workers apart from node.

Anatomy of a command

$
commandhtop

The ncurses process viewer. Install with apt install htop or dnf install htop.

How it works

Like top, htop reads /proc on every refresh (/proc/stat for CPU ticks, /proc/meminfo, and /proc/PID/stat, statm, status and cmdline per process) and computes rates from the difference between samples. It draws with ncurses, so it needs a real terminal; it has no batch mode. Kernel threads are hidden by default and user threads are shown as separate rows (shown in a different colour) unless you hide them with H. Its memory bar is built from /proc/meminfo: green is memory used by processes, blue buffers, yellow/orange page cache. Actions like F9 (kill) and F7/F8 (nice) are plain kill() and setpriority() system calls, with the same permission rules as kill and renice. Settings you change in F2 are saved to ~/.config/htop/htoprc.

Options you'll actually use

-u USERShow only USER's processes
-p PID,...Show only these PIDs
-tTree view (F5 toggles)
-d TENTHSRefresh delay in tenths of a second (-d 10 = 1 s)
-s COLUMNSort by a column, e.g. -s PERCENT_MEM
-HHighlight new and old processes
F3 or /Search for a process by name
F4 or \Filter: show only matching processes
F6 or < >Choose the sort column
F9 or kSend a signal (default SIGTERM) to the selected process
Space / UTag processes / untag all, so F9 acts on all tagged
H / KHide user threads / kernel threads

Common mistakes

htop -d 2 expecting a 2-second refresh

htop's delay is in tenths of a second, so -d 2 refreshes every 0.2 s and burns CPU on a busy box.

htop -d 20
Counting the same process three times because it shows three rows

By default htop lists user threads as separate rows with the same command; RES and VIRT are per process, not per thread.

Press H to hide user threads, or use tree view
htop > snapshot.txt in a script

htop is interactive only and writes screen control codes; there is no batch mode.

top -b -n 1 > snapshot.txt or ps aux --sort=-%cpu
Reading the full Mem bar as 'memory is full'

The bar includes buffers and page cache, which the kernel gives back when needed. The number after it counts used memory only.

Read the 3.87G/7.67G figure, or free -h's available column

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Start htop showing only your own processes, refreshing every second.

  2. 2

    Open htop in tree view and find your shell and htop itself.

  3. 3

    Start `sleep 600 &`, then in htop filter for 'sleep' and send it SIGTERM.

Related commands

topWatch CPU, memory and processes livepsSnapshot the running processeskillSend a signal to a processreniceChange the priority of a running process
PrevioustopNextkill
Linux / Process Management / htop

htop

Interactive process viewer with bars and a tree

Beginnermonitoringinteractiveprocesses

htop is a friendlier top: per-core CPU bars, a memory bar, scrolling, a process tree, search and filter, and killing or renicing with function keys. It is not always installed on servers, but when it is, it is the fastest way to explore a busy box.

Syntaxhtop [-u USER] [-p PID,...] [-t] [-d TENTHS] [-s COLUMN]

Watch it run

CPU alerts fired on a 4-core box. Get a picture of every core and of memory in one screen.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 1
htop

Each numbered bar is one core: green is user time, red kernel time. The Mem bar shows used (green), buffers (blue) and cache (yellow); 3.87G/7.67G counts only real use. The list shows full command lines, so you can tell the three workers apart from node.

Anatomy of a command

$
commandhtop

The ncurses process viewer. Install with apt install htop or dnf install htop.

How it works

Like top, htop reads /proc on every refresh (/proc/stat for CPU ticks, /proc/meminfo, and /proc/PID/stat, statm, status and cmdline per process) and computes rates from the difference between samples. It draws with ncurses, so it needs a real terminal; it has no batch mode. Kernel threads are hidden by default and user threads are shown as separate rows (shown in a different colour) unless you hide them with H. Its memory bar is built from /proc/meminfo: green is memory used by processes, blue buffers, yellow/orange page cache. Actions like F9 (kill) and F7/F8 (nice) are plain kill() and setpriority() system calls, with the same permission rules as kill and renice. Settings you change in F2 are saved to ~/.config/htop/htoprc.

Options you'll actually use

-u USERShow only USER's processes
-p PID,...Show only these PIDs
-tTree view (F5 toggles)
-d TENTHSRefresh delay in tenths of a second (-d 10 = 1 s)
-s COLUMNSort by a column, e.g. -s PERCENT_MEM
-HHighlight new and old processes
F3 or /Search for a process by name
F4 or \Filter: show only matching processes
F6 or < >Choose the sort column
F9 or kSend a signal (default SIGTERM) to the selected process
Space / UTag processes / untag all, so F9 acts on all tagged
H / KHide user threads / kernel threads

Common mistakes

htop -d 2 expecting a 2-second refresh

htop's delay is in tenths of a second, so -d 2 refreshes every 0.2 s and burns CPU on a busy box.

htop -d 20
Counting the same process three times because it shows three rows

By default htop lists user threads as separate rows with the same command; RES and VIRT are per process, not per thread.

Press H to hide user threads, or use tree view
htop > snapshot.txt in a script

htop is interactive only and writes screen control codes; there is no batch mode.

top -b -n 1 > snapshot.txt or ps aux --sort=-%cpu
Reading the full Mem bar as 'memory is full'

The bar includes buffers and page cache, which the kernel gives back when needed. The number after it counts used memory only.

Read the 3.87G/7.67G figure, or free -h's available column

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Start htop showing only your own processes, refreshing every second.

  2. 2

    Open htop in tree view and find your shell and htop itself.

  3. 3

    Start `sleep 600 &`, then in htop filter for 'sleep' and send it SIGTERM.

Related commands

topWatch CPU, memory and processes livepsSnapshot the running processeskillSend a signal to a processreniceChange the priority of a running process
PrevioustopNextkill