LinuxVI · ProcessesObservation tools
ps, pgrep, pkill, top, htop — the production toolkit
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
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:
$ ps auxfUSER 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:
| Convention | Meaning | Standard |
|---|---|---|
ps aux | All processes, user-oriented columns | BSD |
ps -ef | All processes, full-format | System V |
ps -eo ... | Selected columns, every process | Modern |
Pick one and stick to it. ps auxf for the human-readable view,
ps -eo pid,ppid,comm for scripting.
pgrep — search by pattern
$ pgrep nginx; pgrep -u www-data nginx; pgrep -f 'nginx: worker'; pgrep -l nginx1234
1235
1235
1235 nginx: worker processIllustrative output
pkill — kill by pattern
$ pkill -TERM nginx; pkill -HUP -f 'nginx: master'; pkill -KILL -u baduserIllustrative output
top — live snapshot
$ top -bn1 | head -15top - 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 nginxIllustrative output
The CPU breakdown:
| Field | Meaning |
|---|---|
| us | User CPU — time spent running userland code |
| sy | System CPU — time spent in kernel |
| ni | Nice — time spent on nice-priority tasks |
| id | Idle — CPU doing nothing |
| wa | I/O wait — CPU idle waiting for I/O |
| hi | Hardware interrupts |
| si | Software interrupts |
| st | Steal time — CPU taken by the hypervisor |
htop — friendlier top
$ htop -d 5CPU[||||||||||||||||||||||||| 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
- **Use
ps auxf** for the human-readable snapshot - **Use
pgrep -f PATTERN** to confirm what pkill will match before running pkill - **Use
pkill -TERM** first, thenpkill -KILLafter a timeout - **Use
top -bn1** for logable snapshots in runbooks - **Use
htop** for interactive debugging - **Combine with
taskset -p PIDandcat /proc/<pid>/statusfor deeper forensics.
Knowledge check
Knowledge check · 3 questions
Q1. What is the difference between `pgrep nginx` and `pgrep -f nginx`?
Q2. `pkill` asks for confirmation before sending signals.
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.