Skip to content

Lesson 4 of 14 · 20m

Processes, Signals, and Job Control

Ctrl-C sends SIGINT to a process group, `kill` is a request while `kill -9` is a kernel reap, and exit 137 means the OOM-killer chose you.

Ctrl-C doesn't kill a process. It sends SIGINT to the foreground process group attached to your terminal — which is why Ctrl-C on a pipeline sometimes leaves a stage running, and why kill is a request, not a verdict. Signals are the actual mechanism, and they explain almost every confusing process state you'll meet.

The signals that matter

  • SIGINT (2) — Ctrl-C. Asks for interrupt; programs may catch it and clean up.
  • SIGTERM (15) — the default kill sends. A polite request: handlers run, finally/atexit blocks execute, temp files get removed.
  • SIGKILL (9) — the kernel reaps the process. It cannot be caught, blocked, or ignored. No cleanup of any kind: no finally, no buffer flush, no lock release. kill -9 on a process mid-write is how you get truncated databases — it's the last resort, not the default.
  • SIGHUP (1) — "your terminal hung up." Historically kills shell children on logout; daemons repurpose it as a reload signal.
  • SIGTSTP — Ctrl-Z. Suspend, not terminate.

Exit status encodes the signal: 128 + n. A script exiting 130 was SIGINT'd. Exiting 137 means SIGKILL — and on a memory-pressured Linux box, 137 at 02:00 with no log output means the OOM-killer did it. dmesg -T | grep -i oom names the victim. This is a real first-look check, not trivia.

Job control

  • cmd & — start in the background; $! holds its PID.
  • Ctrl-Z then bg — suspend, resume in background. fg %1 brings job 1 back.
  • jobs — list the shell's jobs. %1, %2 reference them.
  • disown %1 — remove the job from the shell's table so it survives logout; without it, a suspended background job gets SIGHUP when you disconnect and dies anyway.
  • nohup cmd & — the pre-tmux answer: ignore SIGHUP, write output to nohup.out.
  • wait — in a script, block until background children finish; wait $! for a specific one.

The mental model to reject: "backgrounded means detached." A background job is still your shell's child attached to your terminal — only disown, nohup, or a multiplexer (tmux) actually severs the hangup path.

Seeing what's running

  • ps aux (BSD syntax) vs ps -ef (SysV) — both still used; ps auxf draws the tree, which is what you actually want when a process spawned helpers.
  • pgrep -fl name, pkill -f pattern — find and signal by name; killall exists, but on Solaris it kills everything, which is why pkill is the habit.
  • kill -- -PGID — signal a whole process group; how you take down a pipeline that spawned helpers.
  • lsof -ti :3000 — the PID listening on a port; killport() { lsof -ti :"$1" | xargs -r kill; } is the function that answers "what's holding 3000."
  • /proc/<pid>/ on Linux: cmdline, cwd, environ, fd/ — the process's real state, readable as files.
  • strace -p <pid> — attach to a hung process and watch the syscall it's parked in. A blocked connect() on a dead host or an flock it will never get is usually the whole answer.
  • <defunct> zombies are already dead — their parent never wait()ed on them. You can't kill them; the fix is in the parent.

Practice

  • kill (TERM) first; -9 only after the process proved it won't exit cleanly.
  • Wrap every unattended job: timeout 300 cmd bounds runtime, flock -n /tmp/job.lock cmd prevents pileups.
  • Read exit codes: 137 → check dmesg for OOM; 130 → interrupted.
  • disown or tmux for anything that must outlive your ssh session.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.