9.2. Controlling Running Programs

Two problems dominate everyday process control: stopping a program without corrupting its work, and keeping a program alive when its session ends. The first is about signals; the second is what terminal multiplexers are for.

9.2.1. Signals and Orderly Termination

A signal is a numbered message the kernel delivers to a process. Most can be caught, ignored, or allowed to do their default damage; the important exceptions are SIGKILL (9), which cannot be caught, and SIGSTOP (19), which cannot be prevented. kill sends them despite its name - list them all with kill -l:

$ kill -l
 1) SIGHUP   2) SIGINT   3) SIGQUIT  4) SIGILL   5) SIGTRAP
 6) SIGABRT  7) SIGBUS   8) SIGFPE   9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG  24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF 28) SIGWINCH    29) SIGIO   30) SIGPWR
31) SIGSYS  34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX    

Four signals do most of the daily work:

Signal Number Default action Typical use
SIGTERM 15 Terminate. The polite "please finish up and exit"; kill's default.
SIGINT 2 Terminate. What Ctrl+C sends to the foreground job.
SIGHUP 1 Terminate. Sent to a session's processes when the terminal closes; daemons reinterpret it as "reload configuration".
SIGKILL 9 Terminate, unconditionally. Last resort: cannot be caught or delayed, so no cleanup runs.

Ctrl+C and the job-control keystrokes were covered in Chapter 3; here the subject is what happens inside the target process. A well-written program treats SIGTERM as a request to finish current work and exit cleanly. This worker script demonstrates the pattern; save it in the workspace as graceful_worker.sh and make it executable with chmod +x graceful_worker.sh:

#!/bin/sh
# Write progress until asked to stop; on TERM, finish the current unit of work.
log=${1:-progress.log}
trap 'echo "TERM received, draining" >>"$log"; echo "worker stopped cleanly" >>"$log"; exit 0' TERM
trap 'echo "INT received, draining" >>"$log"; exit 0' INT
n=0
while :; do
    n=$((n + 1))
    echo "unit $n complete" >>"$log"
    sleep 1
done

It appends one progress line per second to the log named by its first argument. The two trap commands install handlers: on TERM the worker records that it received the signal, writes a final line, and exits with status 0; on INT it does the same minus the final flourish. Run it in the background, wait two seconds, and check the log:

$ nohup ./graceful_worker.sh signal-demo.log >/dev/null 2>&1 &
$ echo $! > worker.pid
$ sleep 2
$ cat signal-demo.log
unit 1 complete
unit 2 complete

The first three commands produce no output: the worker starts in the background, its PID goes into worker.pid, and sleep 2 simply waits. The cat shows the worker has completed two units. Now ask it to stop:

$ kill -TERM $(cat worker.pid)
$ sleep 1
$ cat signal-demo.log
unit 1 complete
unit 2 complete
unit 3 complete
unit 4 complete
unit 5 complete
unit 6 complete
unit 7 complete
TERM received, draining
worker stopped cleanly

kill -TERM sends signal 15; -TERM and -15 are the same thing, and the name is easier to read. The log tells the whole story: five more units completed between the first cat and the signal, then the trap ran, wrote its two closing lines, and the process exited with status 0. That is what "orderly termination" means - the process chose how to finish.

Contrast that with the signal of last resort:

$ nohup sleep 300 >/dev/null 2>&1 &
$ echo $! > kill.pid
$ kill -KILL $(cat kill.pid)
$ ps -o pid,stat,comm -p $(cat kill.pid)
    PID STAT COMMAND

The first three commands are silent setup as before; ps prints only its header. SIGKILL cannot be trapped, so the sleep never ran a line of cleanup code, and ps finds nothing - the PID is gone. The shell answers the kill in its own way: an interactive bash prints the job-status notice [1]+ Killed nohup sleep 300 > /dev/null 2>&1 at its next prompt. A signal death surfaces to a waiting caller as exit status 128 + N for signal number N, so a SIGKILL termination is 137 - Chapter 3 showed the same convention with SIGTERM's 143.

Use SIGKILL only after SIGTERM has had time to fail. A process in uninterruptible sleep (state D) ignores SIGKILL too - not by choice, but because it cannot run to die; Section 9.7.1 covers that case. To signal a process group rather than one process, use the negative PID of the group leader (kill -TERM -PID), which reaches the supervisor and every child at once. Ownership rules apply: you may signal your own processes, and root may signal anyone's.

9.2.2. Keeping Interactive Work Running with tmux

nohup keeps a single command alive past its session, but it gives you no way back in: output goes to a file, and you cannot type at the program again. tmux - installable with $ sudo apt install tmux - solves that by keeping a whole terminal alive on the server and reattaching to it later. Its model is one server process per user, holding any number of sessions, each with windows and panes; your ssh connection is just a viewer.

Start a detached session running a command, and list the sessions:

$ tmux new-session -d -s build "sleep 120"
$ tmux ls
build: 1 windows (created Thu Nov 19 19:42:26 2026)

new-session -d starts the session without attaching to it, -s build names it, and the trailing command becomes the pane's process. tmux ls confirms one session named build. To attach to it interactively, run tmux attach -t build - your terminal now is the pane, showing sleep's blank screen. Leave it with the tmux prefix: press Ctrl+b, release, then d (detach). The session, the sleep, and everything else in it keep running on the server; log out entirely and they continue.

Detached does not mean unreachable. The sleep pane, though, cannot answer typed input, so end that session and start a fresh one whose pane runs an ordinary shell (no command argument):

$ tmux kill-session -t build
$ tmux new-session -d -s build

Both commands are silent: the placeholder session ends, and the new build session starts with a plain shell in its pane. tmux send-keys types into that pane as if you were attached, and tmux capture-pane prints what the pane currently shows. Feed the pane a command once the session is up:

$ tmux send-keys -t build "echo ready" Enter
$ sleep 1
$ tmux capture-pane -p -t build
john@proc:~/debian-handbook/processes$ echo ready
ready
john@proc:~/debian-handbook/processes$

Selected output: the first two commands print nothing, and the capture-pane printout is the pane's content - the shell's own prompt, the echoed command, its output, and the fresh prompt, with trailing blank lines from the pane's height omitted. This is the mechanism worth remembering: the pane is a real terminal with a real shell, whether or not anyone is looking at it.

The everyday keys, all prefixed with Ctrl+b:

Key Effect
d Detach; the session keeps running.
c / n / p New window / next / previous window.
% / " Split the window side by side / top and bottom.
[ Enter copy mode (scroll with arrows; q leaves).
? List all keys.

Detach leaves a session running; kill-session ends it for real:

$ tmux kill-session -t build
$ tmux ls
no server running on /tmp/tmux-1001/default

kill-session terminates the session; with no sessions left, the server exits too, and tmux ls reports that with status 1. The socket path names the account - /tmp/tmux-1001 is john's UID 1001 - so every user has a private tmux server. A pane whose command exits ends its window; a session whose last window closes ends itself. In normal use you start one long-lived session per host (tmux new -s work), work inside it, detach on logout, and tmux attach -t work on the next login.