Processes, signals and job control
Move from a laptop shell to a safe, observable delivery pipeline: version control, Linux processes and permissions, container images, networking, configuration and secrets, deployment, health checks, observability, rollback, reproducibility and security hardening.
What you will be able to do
#- Describe the process tree, parent and child relationships and process states.
- Explain what SIGTERM, SIGKILL and SIGHUP do and when each is appropriate.
- Manage foreground and background jobs and find the process owning a port.
Overview
#A process is a running program with its own memory and a numeric process ID (PID). The kernel starts PID 1 at boot, and every other process descends from it. Understanding this tree explains why killing a parent can orphan or reap children.
Inspecting processes
ps aux # every process with CPU and memory
ps -ef --forest # show the parent/child tree
top # live view; press q to quit
pgrep -a node # PIDs and command lines matching node
Each process has a state: running, sleeping, stopped, or zombie. A zombie has finished but its parent has not read its exit status; it holds a PID slot but no memory.
Signals
A signal is a small asynchronous message to a process. The ones you will use:
SIGTERM(15) asks the process to shut down cleanly. It can be caught to run cleanup.SIGKILL(9) cannot be caught; the kernel destroys the process immediately.SIGHUP(1) traditionally means the controlling terminal closed; many daemons reload config on it.SIGINT(2) is what Ctrl-C sends.
kill -TERM 1234 # polite shutdown request
kill -9 1234 # last resort
Prefer SIGTERM first so the process can flush buffers and close connections. Reserve SIGKILL for a process that ignores SIGTERM.
Jobs and ports
npm start & # run in background
jobs # list background jobs
fg %1 # bring job 1 to the foreground
lsof -i :3000 # which process holds port 3000
Common pitfalls
- Killing a process group by name and taking down unrelated services that share the name.
- Using
SIGKILLfirst and losing in-flight writes. - Leaving background jobs running after a script exits, so the port stays occupied.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
A Node server has a SIGTERM handler that closes the HTTP server and then calls process.exit(0). A container sends SIGTERM, then SIGKILL five seconds later. What happens if the server takes eight seconds to drain connections?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.javascriptprocess.on('SIGTERM', () => server.close(() => process.exit(0)));Exercise 2Implement
Write the steps to find and gracefully stop the process listening on port 8080 without killing unrelated processes.
starter.bash# identify the PID, then stop it politelyReveal the reference answer
reference.bashPID=$(lsof -t -i :8080) kill -TERM "$PID" # wait, then verify sleep 2 lsof -i :8080 || echo "port free"Authored expected output: The specific PID owning port 8080 receives SIGTERM; the port is free afterwards.
lsof -t returns only the PID for that socket, so you target the exact process rather than matching a name that other processes might share.
Exercise 3Explain
Explain why killing a parent process can leave orphaned children and how a process group helps.
starter.text// Describe parent/child relationships and process groups.Reveal the reference answer
reference.textChildren are not killed automatically when a parent dies; they are re-parented to PID 1 or a subreaper and keep running. A process group lets you signal the whole tree at once: kill -TERM -PGID sends the signal to every process in the group, so a supervisor can shut down a service and all of its workers together.Authored expected output: An explanation that orphans survive a parent death and that a process group signals the whole tree.
This is why container runtimes and supervisors track process groups or cgroups rather than a single PID when stopping a service.
Common misconceptions
#- SIGKILL is just a stronger SIGTERM: SIGKILL cannot be handled at all, so no cleanup runs.
- A zombie process consumes CPU: a zombie uses no CPU and only a small process-table entry.
Follow-up practice
#- devops-permissions-users
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.