Signals and stopping processes
On this page
A waiting command still owns a temporary file. Ending its process might leave that file behind, even though the process disappears from a listing. You need to distinguish asking a program to finish from preventing it from running any more code.
By the end, you can interrupt a foreground command, request cleanup with TERM, and explain why KILL can end execution without removing files.
Before you start
Section titled “Before you start”Finish Inspecting activity and resources and its reset. Work in the B04 lab directory, using only disposable ~/lab/b13 and processes you start yourself. No sudo is needed.
Your PIDs and parent PIDs will differ. These recordings use a lab container with few processes and a Python capture program as PID 1, not systemd. Never signal PID 1, your interactive shell, or a PID copied from a recording. Save and inspect your own worker’s PID instead.
Interrupt the command using the terminal
Section titled “Interrupt the command using the terminal”sleep 60While it is waiting, press Ctrl+C. The terminal sends an interrupt signal, INT, to its foreground process group. For this simple command, that includes sleep. Sleep’s default response ends it, and Bash gives you another prompt.
learner@rootvolt:~/lab$ sleep 60^Clearner@rootvolt:~/lab$ Steps (3)
- Type
sleep 60 - Press Enter
- Press Ctrl + C
A signal is a notification delivered to a process. Many signals have default actions, and a program can arrange handlers for some of them. INT usually means that the terminal user wants the foreground work interrupted. It is not a request to erase files. A program’s handler can change its response.
Prepare a worker that can clean up
Section titled “Prepare a worker that can clean up”TERM is a termination request. Unlike KILL, it permits a program to run a handler before ending. The command named kill sends signals; its default signal is TERM, not KILL.
Create a waiting input channel:
mkdir -p ~/lab/b13mkfifo ~/lab/b13/inboxmkfifo creates a named pipe, a path used to pass input between processes rather than store ordinary file contents. This worker waits for a line on that pipe, without a CPU loop or helper process. Why not simply sleep 300 inside the script? While Bash waits for a command such as sleep to finish, it holds back a trapped signal until that command ends, so the cleanup would run minutes late. Bash’s own read lets the trap run at once. Use your text editor to create ~/lab/b13/cooperative.sh with these exact lines:
file="$HOME/lab/b13/work.tmp"exec 3<> "$HOME/lab/b13/inbox"trap 'rm "$file"; printf "TERM: removed work.tmp\n"; exit 0' TERMprintf 'temporary data\n' > "$file"read -r -t 300 -u 3rm "$file"This is a supplied worker, not a scripting exercise. Read its responsibilities before running it:
filenames its ordinary temporary file.exec 3<>opens the named pipe for reading and writing on file descriptor 3. B09 introduced descriptors 0, 1, and 2; this is an additional open channel.trapregisters the quoted commands as its TERM handler: remove the temporary file, report that removal, and exit successfully.printfwrites the temporary data.readwaits for a line on descriptor 3, for at most 300 seconds. Keeping the pipe open for writing also prevents an immediate end-of-input.- If the wait ends normally, the last line removes the file too.
Run the comparisons before that five-minute limit. Do not write to the pipe during the exercise. Calling bash runs the file without needing an executable permission bit.
Request TERM and wait for completion
Section titled “Request TERM and wait for completion”bash ~/lab/b13/cooperative.sh &process=$!ps -p "$process" -o pid,ppid,commls ~/lab/b13/work.tmpkill "$process"; wait "$process"test ! -e ~/lab/b13/work.tmp && printf 'File absent after TERM\n'The & starts the worker and returns the prompt at once; B13.4 explains background jobs in full. $! saves its PID. This worker appears as bash, because Bash interprets its script. The listing and file check establish that your instance and its temporary file exist before you signal it.
The semicolon runs two commands in sequence. kill requests termination; wait waits for this child to finish and collects its status. Sending a signal successfully does not itself prove completion or successful cleanup. Wait and inspect the result.
learner@rootvolt:~/lab$ bash ~/lab/b13/cooperative.sh &[1] 22learner@rootvolt:~/lab$ process=$!learner@rootvolt:~/lab$ ps -p "$process" -o pid,ppid,comm PID PPID COMMAND
22 11 bashlearner@rootvolt:~/lab$ ls ~/lab/b13/work.tmp/home/learner/lab/b13/work.tmplearner@rootvolt:~/lab$ kill "$process"; wait "$process"TERM: removed work.tmp
[1]+ Done bash ~/lab/b13/cooperative.shlearner@rootvolt:~/lab$ test ! -e ~/lab/b13/work.tmp && printf 'File absent after TERM\n'File absent after TERMlearner@rootvolt:~/lab$ Steps (17)
- Type
bash ~/lab/b13/cooperative.sh & - Press Enter
- Output:
[1] 22 - Type
process=$! - Press Enter
- Type
ps -p "$process" -o pid,ppid,comm - Press Enter
- Output:
PID PPID COMMAND 22 11 bash - Type
ls ~/lab/b13/work.tmp - Press Enter
- Output:
/home/learner/lab/b13/work.tmp - Type
kill "$process"; wait "$process" - Press Enter
- Output:
TERM: removed work.tmp [1]+ Done bash ~/lab/b13/cooperative.sh - Type
test ! -e ~/lab/b13/work.tmp && printf 'File absent after TERM\n' - Press Enter
- Output:
File absent after TERM
The captured handler prints TERM: removed work.tmp. Bash prints a Done job notice during the combined kill-and-wait command. The absence test then confirms the file is gone. That is evidence of this worker’s cooperation, not a promise that every program handles TERM identically.
Compare forceful termination in a separate run
Section titled “Compare forceful termination in a separate run”Start another instance and establish its file exists. The same line can start it and save $!:
bash ~/lab/b13/cooperative.sh & process=$!test -f ~/lab/b13/work.tmp && printf 'File exists before KILL\n'Predict first: if the kernel ends the worker before it can run its trap, will the file disappear anyway?
kill -KILL "$process"; wait "$process"ps -p "$process" -o pid,commcat ~/lab/b13/work.tmpRun it, then compare with your guess
learner@rootvolt:~/lab$ bash ~/lab/b13/cooperative.sh & process=$![1] 26learner@rootvolt:~/lab$ test -f ~/lab/b13/work.tmp && printf 'File exists before KILL\n'File exists before KILLlearner@rootvolt:~/lab$ kill -KILL "$process"; wait "$process"[1]+ Killed bash ~/lab/b13/cooperative.shlearner@rootvolt:~/lab$ ps -p "$process" -o pid,comm PID COMMANDlearner@rootvolt:~/lab$ cat ~/lab/b13/work.tmptemporary datalearner@rootvolt:~/lab$ Steps (15)
- Type
bash ~/lab/b13/cooperative.sh & process=$! - Press Enter
- Output:
[1] 26 - Type
test -f ~/lab/b13/work.tmp && printf 'File exists before KILL\n' - Press Enter
- Output:
File exists before KILL - Type
kill -KILL "$process"; wait "$process" - Press Enter
- Output:
[1]+ Killed bash ~/lab/b13/cooperative.sh - Type
ps -p "$process" -o pid,comm - Press Enter
- Output:
PID COMMAND - Type
cat ~/lab/b13/work.tmp - Press Enter
- Output:
temporary data
No. Bash reports Killed, and wait returns 137 in this run. The process is absent, but its file remains. The TERM cleanup message never appears.
The kernel still ends the process and closes its open file descriptors, including descriptor 3. Closing descriptors does not automatically unlink existing names on disk. work.tmp remains an ordinary named file until a process explicitly removes it. The named pipe’s path remains too; closing the worker’s channel does not remove that path.
In words: TERM reaches the cooperative worker, its trap removes the file, and its exit lets the kernel close descriptors. KILL makes the kernel end execution without the trap, so descriptor cleanup occurs while the file remains. The model omits scheduling and other resources; it compares two separate runs, not two signals sent to one surviving process.
Mistakes to recognize
Section titled “Mistakes to recognize”Assuming kill always forces termination. With no signal option, Bash’s kill sends TERM. -KILL explicitly chooses the forceful signal, also known numerically as 9.
Treating a sent signal as proof that work was saved. Check completion and application-specific evidence. Programs can handle or ignore TERM; successful delivery is not a cleanup receipt.
Reading Done as a universal signal outcome. This worker handles TERM and exits 0, so Bash reports Done. The sleeps in earlier lessons instead used TERM’s default action and were reported as Terminated.
Try it yourself
Section titled “Try it yourself”Goal: Explain why an absent process in ps and an absent temporary file are different pieces of evidence. Which one did KILL establish here, and which one required the TERM handler?
Solution
Here ps establishes absence of the ended process, not a suspended state or file cleanup. KILL ended the worker, while its file remained readable. The TERM handler explicitly removed the file before exiting. Both paths let the kernel close descriptors; neither descriptor closure nor process disappearance proves pathname removal.
Reset this lesson
Section titled “Reset this lesson”The forceful comparison deliberately leaves a file. Remove only this exercise’s paths:
cd ~/lab/b13rm work.tmp inbox cooperative.shcd ..rmdir ~/lab/b13unset processThe recorded workers have both ended and were collected by wait. The reset removes the leftover regular file, the named pipe, and the script, then their empty directory. If you stopped midway with a worker still alive, request TERM for its saved PID and wait before removing its input channel.
learner@rootvolt:~/lab$ cd ~/lab/b13learner@rootvolt:~/lab/b13$ rm work.tmp inbox cooperative.shlearner@rootvolt:~/lab/b13$ cd ..learner@rootvolt:~/lab$ rmdir ~/lab/b13learner@rootvolt:~/lab$ unset processlearner@rootvolt:~/lab$ Steps (10)
- Type
cd ~/lab/b13 - Press Enter
- Type
rm work.tmp inbox cooperative.sh - Press Enter
- Type
cd .. - Press Enter
- Type
rmdir ~/lab/b13 - Press Enter
- Type
unset process - Press Enter
- Ctrl+C sends INT to foreground work; a program’s response depends on its signal handling.
kill PIDrequests TERM, allowing a cooperative handler to finish cleanup.- KILL ends execution without user-space handlers; reserve it for cases where cooperation fails.
- The kernel closes descriptors after termination, but existing files are not automatically deleted.
Next, Jobs and background work explains how to suspend and resume a waiting command.
Further reading
Section titled “Further reading”- In Bash:
help kill,help trap,help wait, andhelp read. On Ubuntu:man 7 signalandman mkfifo. - Ubuntu signal manual.
- GNU Bash: Signals and Bourne shell builtins.