Handbooks / Linux / Chapter 4

Processes and Signals

7 pages · ~5 min✓ Reviewed

Builds on Shell Scripting. Next up: Networking Commands.

LinuxPage 1

Process Mental Model

1. Process and STAT

  • Process = running program with a PID and PPID (parent)
  • Every process has a parent; PID 1 (init/systemd) is the ancestor
STATMeaning
Rrunning
Ssleeping
DI/O wait, cannot be killed
Tstopped
Zzombie: exited, parent has not called wait

2. Control layers

  1. Signalkernel's message
  2. Jobshell handle
  3. Servicesystemd: logs, restart
  • Foreground jobs own the terminal
  • Background jobs die on hangup unless nohup or disown

3. Interview Point

  • Zombie vs orphan? Zombie: exited, parent never called wait. Orphan: parent died, child still runs
  • Why can't kill -9 remove a zombie? Already dead; only the parent's wait reaps it
  • PID 1 and orphans? It adopts them and reaps them on exit
LinuxPage 2

Inspecting Processes with ps and top

1. ps vs top

  • ps = one-time snapshot, use it in scripts
  • top = live refresh, use it to watch load
  • ps aux (BSD): USER PID %CPU %MEM VSZ RSS STAT COMMAND
  • ps -ef (POSIX): adds PPID and the full command line

2. Filtering

  • ps -p PID shows one process
  • pgrep -a nginx lists PID and command by name
  • ps aux | grep [n]ginx: the [n] trick stops grep matching itself

3. top keys and load

KeyAction
Psort by CPU
Msort by memory
1per-core CPU
kkill a PID
qquit
  • Load average = 1, 5, 15 min values
  • Load above the core count = saturated

4. Interview Point

  • VSZ vs RSS? VSZ is virtual size; RSS is real RAM in use
  • Top memory user? ps aux --sort=-%mem | head, or M in top
  • Load average? Runnable and waiting tasks, averaged over 1/5/15 min
LinuxPage 3

Jobs: Foreground and Background

1. Controlling Jobs

ActionKeys / commandSignal
Start in backgroundcmd &none
Suspend foregroundCtrl+ZSIGTSTP
Interrupt foregroundCtrl+CSIGINT
  • jobs lists this shell's jobs as [1]+
  • fg %1 brings job 1 to the foreground
  • bg %1 resumes a stopped job in the background
  1. Run long cmd
  2. Ctrl+Zstopped
  3. bgkeeps running
  4. fgback on terminal

2. Surviving Hangup

  • Closing the terminal sends SIGHUP, which kills background jobs
  • nohup cmd & ignores HUP; output goes to nohup.out
  • disown -h %n protects a job that is already running
bash
nohup ./backup.sh &   # job %1
sleep 600 &           # job %2
disown -h %2          # already running

disown -h takes the job number

Only nohup writes nohup.out

disown -h just stops SIGHUP. Output still goes to the closed terminal.

3. Interview Point

  • Foreground to background? Ctrl+Z, then bg
  • Why do jobs die on SSH close? SIGHUP. Use nohup or disown -h
  • jobs vs ps? jobs shows this shell's jobs; ps shows system processes
LinuxPage 4

Signals, kill and Exit Codes

1. Signals and kill

  • kill PID sends SIGTERM (15): catchable, so the process cleans up and exits
  • kill -9 PID sends SIGKILL: cannot be caught or ignored, no cleanup
NumSignalMeaning
1HUPreload config / hangup
2INTCtrl+C
9KILLuncatchable
15TERMdefault, catchable
18CONTresume
19STOPpause, uncatchable
20TSTPCtrl+Z

2. Kill by name

  • killall name: by process name
  • pkill -f pattern: by full command line
  • Preview first with pgrep -a pattern: matches can hit unrelated processes

3. Exit codes

CodeMeaning
0success
nonzerofailure
126not executable
127command not found
128+Nkilled by signal N
137 / 143SIGKILL / SIGTERM
echo $?last command's code

4. Interview Point

  • Why TERM before KILL? TERM lets the process clean up; KILL skips cleanup
  • What does 137 mean? 128+9: killed by SIGKILL, often OOM or a stop timeout
  • Did the last command succeed? echo $?, and 0 means yes
LinuxPage 5

systemd Services and journalctl

1. Unit Files

  • PID 1 starts and supervises every service
  • Unit file: /etc/systemd/system/app.service
  • [Unit] ordering, [Service] how to run, [Install] boot target
ini
[Unit]
After=network.target
[Service]
ExecStart=/usr/bin/app
User=app
Restart=on-failure
[Install]
WantedBy=multi-user.target

2. Lifecycle

CommandEffect
start / stopnow only, gone after reboot
enable / disableboot only, not started now
enable --nowenable + start
restartstop, then start a new process
reloadre-read config, same process
  • Edited a unit? Run systemctl daemon-reload
  • systemctl status app shows state; systemctl --failed lists failures

3. Logs with journalctl

FlagShows
-u appone service's logs
-ffollow live
-n 100last 100 lines
--since '1 hour ago'recent window
-p errerrors and worse
-bcurrent boot only
  • Crash loop: systemctl status app, then journalctl -u app -b -p err
start is not enable

start does not survive a reboot, and enable does not start now. Use enable --now for both.

LinuxPage 6

Process Control Cheat Sheet

1. Inspect

  • ps aux / ps -ef: snapshot; pgrep -a name: find PIDs
  • top keys: P CPU, M mem, 1 cores, k kill, q quit
  • STAT: R run, S sleep, D I/O wait, T stopped, Z zombie

2. Jobs and signals

bash
nohup ./backup.sh &   # job %1, survives hangup
sleep 600 &           # job %2
disown -h %2          # already running job
jobs                  # Ctrl+Z, bg %n, fg %n
CommandEffect
kill PIDSIGTERM (15)
kill -9 PIDlast resort
kill -HUP PIDreload
pkill -f patternmatch full command line
killall namekill by name

3. Exit codes and systemd

CodeMeaning
0ok
1general error
126not executable
127not found
130Ctrl+C
137SIGKILL
143SIGTERM
  • Read the last code with echo $?
  • systemctl start|stop|restart|status app
  • enable --now = boot + start; daemon-reload after unit edits
bash
journalctl -u app -f -n 100 --since '1 hour ago' -p err
LinuxPage 7

Check yourself

What does this print?
  • 137
  • 128 + 9, because SIGKILL is signal 9
sleep 60 &
kill -9 $!
wait $!
echo $?
You edited app.service and ran systemctl restart app, but the old behaviour remains. What did you miss?
  • systemctl daemon-reload
  • Without it, systemd keeps the old unit definition
ps aux | grep nginx shows a line, yet nginx is not running. Why?
  • grep matches its own command line
  • Use grep [n]ginx or pgrep -a nginx