Skip to main content
RunBook Academy

LinuxVI · ProcessesObservation tools

ps, pgrep, pkill, top, htop — the production toolkit

Foundation⏱ ~10 minbashpspgreppkilltop

What you'll learn

  • Use ps with the right flags for any production diagnostic
  • Distinguish ps, pgrep, pkill, top, htop
  • Read CPU, memory, and per-thread metrics from top
  • Avoid the common pgrep/pkill mistakes

Prerequisites

Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09

Not yet marked complete on this device.

The five commands in this lesson cover 95% of production process diagnostics. The discipline is knowing the right one for the question.

ps — snapshot

ps reads /proc and prints a snapshot of running processes. The flags determine what to show:

Read-only / Safeps auxf
$ ps auxf
USER  PID %CPU %MEM   VSZ   RSS TTY   STAT START   TIME COMMAND
root    1  0.0  0.1  16892 12096 ?    Ss   Jul19   0:05 /sbin/init
root  567  0.0  0.1  23456  4096 ?    Ss   Jul19   0:01 /usr/sbin/cron -f
www-d 1234  2.0  1.1 1234567 45678 ?    S    Jul19   5:23 nginx: master process /etc/nginx/nginx.conf
www-d 1235  0.5  1.1 1234567 45678 ?    S    Jul19   1:23 nginx: worker process
...

Illustrative output

The flag conventions are confusing because they predate each other:

ConventionMeaningStandard
ps auxAll processes, user-oriented columnsBSD
ps -efAll processes, full-formatSystem V
ps -eo ...Selected columns, every processModern

Pick one and stick to it. ps auxf for the human-readable view, ps -eo pid,ppid,comm for scripting.

pgrep — search by pattern

Read-only / Safepgrep
$ pgrep nginx; pgrep -u www-data nginx; pgrep -f 'nginx: worker'; pgrep -l nginx
1234
1235
1235
1235 nginx: worker process

Illustrative output

pkill — kill by pattern

Service impact possiblepkill
$ pkill -TERM nginx; pkill -HUP -f 'nginx: master'; pkill -KILL -u baduser

Illustrative output

top — live snapshot

Read-only / Safetop
$ top -bn1 | head -15
top - 12:00:00 up 30 days,  3:45,  1 user,  load average: 4.20, 3.10, 2.50
Tasks: 234 total,   3 running, 220 sleeping,   0 stopped,   0 zombie
%Cpu(s): 12.5 us,  0.0 sy,  0.0 ni, 87.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :  16000.0 total,   1234.5 free,   8901.2 used,   5864.3 buff/cache

PID USER   PR  NI   VIRT   RES   SHR  S  %CPU  %MEM     TIME+  COMMAND
12345 root   20   0  1234m  456m  123m  R  95.0   1.1   0:42.15  nginx
12346 www-d  25   5  1234m  456m  123m  S   2.0   1.1   0:01.20  nginx
12347 www-d  25   5  1234m  456m  123m  S   1.5   1.1   0:01.05  nginx
12348 www-d  25   5  123m  45m  12m  S   0.5   0.1   0:00.30  nginx

Illustrative output

The CPU breakdown:

FieldMeaning
usUser CPU — time spent running userland code
sySystem CPU — time spent in kernel
niNice — time spent on nice-priority tasks
idIdle — CPU doing nothing
waI/O wait — CPU idle waiting for I/O
hiHardware interrupts
siSoftware interrupts
stSteal time — CPU taken by the hypervisor

htop — friendlier top

Read-only / Safehtop
$ htop -d 5
CPU[|||||||||||||||||||||||||     70.0%]   Tasks: 234, 567 thr; 1 running
Mem[|||||||||||||             45.0%/16G]   Load average: 4.20 3.10 2.50
Swp[|                       0K/2G]         Uptime: 30 days, 03:45

PID USER   PRI  NI  VIRT   RES   SHR  S  CPU%  MEM%   TIME+  Command
12345 root    20   0  1234m  456m  123m R  95.0   1.1  0:42.15  nginx: master
12346 www-d   25   5  1234m  456m  123m S   2.0   1.1  0:01.20  nginx: worker
...

Illustrative output

Production discipline

  1. **Use ps auxf** for the human-readable snapshot
  2. **Use pgrep -f PATTERN** to confirm what pkill will match before running pkill
  3. **Use pkill -TERM** first, then pkill -KILL after a timeout
  4. **Use top -bn1** for logable snapshots in runbooks
  5. **Use htop** for interactive debugging
  6. **Combine with taskset -p PID and cat /proc/<pid>/status for deeper forensics.

Knowledge check

Knowledge check · 3 questions

  1. Q1. What is the difference between `pgrep nginx` and `pgrep -f nginx`?

  2. Q2. `pkill` asks for confirmation before sending signals.

  3. Q3. Which of the following are correct practices for production process management? Select all that apply.

Passing score: 75%. Answers are checked in this browser.