Kiet Nguyen logo
NotesNotesResumeResume
© 2026 Kiet Nguyen
← All categories

07

Processes & Signals

  • Find which one burns CPU/RAM
  • Stop a stuck process safely (TERM then KILL)
  • Explain zombie vs running; avoid ps | grep self-match
pstophtoppgreppkillkillpstreejobs

Must-know cold

  • ps aux · ps aux --sort=-%mem | head · pgrep -ax name
  • kill PID (SIGTERM) → wait → kill -9 PID (last resort)
  • top / htop · STAT: R run/runnable S interruptible sleep D uninterruptible (often I/O) T stopped Z zombie
  • Prefer pgrep -x / pgrep -f over ps aux | grep

Common signals

Definition: Frequently used signals. Numbers below are Linux x86/ARM (use names in scripts). KILL and STOP cannot be caught, blocked, or ignored.

SignalNumber (Linux)Meaning
HUP1Hangup / controlling TTY gone; many daemons reload
INT2Interrupt (Ctrl-C); can be caught
QUIT3Quit (Ctrl-\); dump core by default
KILL9Force kill; not catchable
TERM15Polite terminate (default kill); can be caught
CONT18Continue after STOP/TSTP
STOP19Pause; not Ctrl-Z; not catchable
TSTP20Terminal stop (Ctrl-Z); can be caught

Commands

ps

Definition: Snapshot of running processes (PID, CPU, memory, command).

OptionArgumentMeaningExample
aux—All users, user format, no-tty toops aux
-ef—Full format; shows PPIDps -ef
-uUSERProcesses of USERps -u jenkins
-pPIDOnly this PIDps -p 1234
-oCOLSCustom columnsps -o pid,ppid,stat,cmd
--sortKEYSort (-%mem, -%cpu). %cpu is lifetime CPU/elapsed, not a live sampleps aux --sort=-%cpu
-CNAMEBy command nameps -C nginx
f / --forest—ASCII process treeps auxf
-L—Show threads (with other opts)ps -eLo pid,tid,comm

Useful columns (-o): pid ppid user %cpu %mem rss stat etime cmd nlwp

Flag combos

ComboMeaningExample
ps aux --sort=-%mem | head -15Top RAMIncident start
ps aux --sort=-%cpu | head -15Highest lifetime %CPULive burn: top / htop
ps -fp PIDFull line for one PID
ps -o pid,ppid,stat,wchan,cmd -p PIDState + wait channelHung process

top

Definition: Live, refreshing view of process resource usage.

OptionArgumentMeaningExample
(none)—Live process viewtop
-b—Batch mode (scriptable)top -b -n 1
-nNNumber of iterationstop -b -n 1
-pPIDWatch specific PIDtop -p 1234
-uUSEROnly USERtop -u jenkins
-dSECDelay between refreshtop -d 2
-H—Show threadstop -H
-oFIELDSort field (Linux)top -o %MEM

Inside top:

  • P CPU sort
  • M MEM sort
  • k kill
  • q quit
  • 1 per-CPU
  • c full cmd

Flag combos

ComboMeaningExample
top -b -n 1 | head -30Snapshot for ticketPasteable

htop

Definition: Interactive process viewer (friendlier top). Optional package — install if missing.

OptionArgumentMeaningExample
(none)—Friendlier interactive tophtop
-pPIDSpecifichtop -p 1,2
-uUSERFilter userhtop -u root
-t—Tree viewhtop -t

Flag combos

ComboMeaningExample
htop -u jenkinsAgent processesCI host

pgrep

Definition: Find process IDs by ERE against the process name (/proc/pid/stat, historically 15 chars) or, with -f, the full command line.

OptionArgumentMeaningExample
(none)PATTERNPIDs whose name matches the ERE (ssh also hits sshd)pgrep nginx
-xPATTERNExact name (or exact cmdline with -f)pgrep -x nginx
-aPATTERNPID + full command line (pgrep only)pgrep -a java
-fPATTERNMatch full cmdline, not only the 15-char namepgrep -f agent.jar
-uUSEREffective UIDpgrep -u jenkins
-lPATTERNPID + process namepgrep -l sshd
-cPATTERNCount matchespgrep -c java
-nPATTERNNewest onlypgrep -n python
-oPATTERNOldest onlypgrep -o python
-dDELIMDelimiter between PIDspgrep -d, java

Flag combos

ComboMeaningExample
pgrep -af jenkinsSee matching command lines
pgrep -x nginxExact comm, not a substring/regex surprise
ps -fp $(pgrep -d, nginx)Full ps for matches

pkill

Definition: Send a signal to processes matched by name/pattern.

OptionArgumentMeaningExample
(none)PATTERNSignal matching processes (default TERM)pkill sleep
-fPATTERNFull cmdline matchpkill -f 'sleep 999'
-uUSEREffective UIDpkill -u bob
-signalPATTERNSignal by name/numberpkill -TERM java
-9PATTERNSIGKILLpkill -9 -f stuck
-cPATTERNPrint match count — still sends the signal (procps)Not a dry-run
-n / -oPATTERNNewest/oldest onlypkill -n worker

Flag combos

ComboMeaningExample
pkill -TERM -f 'worker.js'Polite by cmdlinePrefer before -9
pkill -u malwareAll of user (careful)

kill

Definition: Send a signal to a process by PID (default SIGTERM).

OptionArgumentMeaningExample
(none)PIDSend SIGTERM (15)kill 1234
-sSIG PIDSignal by namekill -s TERM 1234
-SIGPIDSignal shorthandkill -TERM 1234
-9PIDSIGKILLkill -9 1234
-l—List signalskill -l
-0PIDSignal 0: success if the PID exists and you may signal itkill -0 1234

Flag combos

ComboMeaningExample
kill PID; sleep 2; kill -0 PID || echo goneTERM then probe (EPERM also fails -0)
kill -9 PIDLast resortAfter TERM fails

pstree

Definition: Show running processes as a parent/child tree.

OptionArgumentMeaningExample
(none)—Process treepstree
-p—Show PIDspstree -p
-sPIDParents of PIDpstree -sp 1234
-u—Show uid transitionspstree -u
-a—Command line argspstree -a

Flag combos

ComboMeaningExample
pstree -sp PIDWho owns this child treeBefore killing children

jobs

Definition: Manage shell background/foreground jobs and hangup-resistant tasks (bg, fg, nohup).

Command / optionArgumentMeaningExample
cmd &—Run in backgroundsleep 300 &
jobs—List shell jobsjobs -l
jobs -l—Include PIDsjobs -l
fg%NForeground job Nfg %1
bg%NResume stopped job in backgroundbg %1
disown%NDetach job from shelldisown %1
nohup cmd—Ignore SIGHUP and redirect TTY stdout/stderr (default nohup.out). Does not setsid / daemonizenohup ./job &
nohup cmd >f 2>&1 &—Redirect properlyPreferred

Flag combos

ComboMeaningExample
nohup ./run.sh >run.log 2>&1 &Survive logout (still not systemd)Lab only

Common recipes

GoalCommand
Top memoryps aux --sort=-%mem | head -15
Top CPUps aux --sort=-%cpu | head -15
Find by namepgrep -ax nginx
Graceful killkill $(pgrep -x myapp)
Force killkill -9 PID after TERM
Treepstree -sp PID
Live viewtop or htop
Exists and signalable?kill -0 PID && echo yes

Pitfalls

  • kill -9 first → skips cleanup (locks, temp files) — last resort.
  • Ctrl-Z is SIGTSTP, not SIGSTOP. SIGSTOP/SIGKILL cannot be caught.
  • pgrep/pkill patterns are EREs against the short comm unless you use -x or -f. pgrep ssh matches sshd.
  • pkill -c still delivers the signal; it is not a dry-run. List signals with kill -l, not pkill -l.
  • Linux killall name signals every process with that comm. On some Unixes killall means “kill everything.” Prefer a PID or pkill -x.
  • Zombie (Z): fix/restart the parent; kill -9 on the zombie does nothing.
  • D state: uninterruptible sleep (often I/O/NFS); the process may not die until the kernel wait ends.
  • kill -0 fails with EPERM if the PID exists but you cannot signal it — that is not “dead.”
  • ps %CPU is lifetime CPU/elapsed, not the live burn top shows.
  • Production long-running services → systemctl stop, not raw kill (see sheet 08).

Process actions map to C system calls, CLI commands, and POSIX signals across four phases: lifecycle, signals, job control, observability.

Creation & lifecycle

VerbC syscall / toolWhat it does
Forkfork()Clones the parent into a child with duplicate FDs and memory state.
Execexecve()Replaces the current process image with a new binary (same PID).
Spawnposix_spawn()Combines fork + exec in one operation.
Daemonizedaemon() / setsidDouble-fork / new session, detach from the controlling TTY. nohup is not this — it only ignores SIGHUP and redirects output.
Orphan—Parent exits first; child is adopted by init (PID 1) or systemd.
Reapwait() / waitpid()Parent reads the child’s exit status and frees the process-table slot.
Zombie—Terminated process whose exit status has not yet been reaped.

Signal & termination

VerbSignal / commandWhat it does
Signalkill -<SIG> <PID>Asynchronous event from the kernel to a process.
InterruptSIGINT (2) / Ctrl-CDefault is terminate; can be caught or ignored.
HangupSIGHUP (1)Controlling TTY gone; daemons often reload config.
TerminateSIGTERM (15)Polite exit; time to close files and sockets.
Kill (force)SIGKILL (9)Immediate kernel destroy; cannot be caught.
QuitSIGQUIT (3) / Ctrl-\Terminate and dump core for debugging.

Job control & state

VerbCommand / signalWhat it does
Suspend / pauseSIGTSTP (20) / Ctrl-ZJob-control stop; can be caught. SIGSTOP (19) also stops, but cannot be caught and is not Ctrl-Z.
ResumeSIGCONT (18) / bg / fgContinues a STOP/TSTP’d process (bg/fg send CONT as needed).
Renice / prioritizenice / reniceChanges niceness (−20 highest … 19 lowest).
Pin / affinetasksetLocks a process to specific CPU cores.
Limit / throttlecgroups / ulimitCaps RAM, CPU, or open file descriptors.

Inspection & observability

VerbCommand / toolWhat it does
Inspect / queryps, pgrep, pidofReads /proc/[pid]/ for state, owner, cmdline.
Monitor / toptop, htop, btopContinuous sample of CPU, memory, threads.
Tracestrace, ltraceLogs syscalls or library calls.
Attachgdb -p <PID>Debugger/profiler via ptrace().

For more details, try man <command> in your terminal.

Previous06 Text ProcessingNext08 systemd Services