Skip to content

Signals and stopping processes

Module
Processes and System Information
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21
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.

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.

Terminal window
sleep 60

While 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.

Interrupt a foreground wait
learner@rootvolt:~/lab$ sleep 60^Clearner@rootvolt:~/lab$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Ctrl+C interrupts the foreground sleep; the capture records its completion status as 130.
Steps (3)
  1. Type sleep 60
  2. Press Enter
  3. 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.

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:

Terminal window
mkdir -p ~/lab/b13
mkfifo ~/lab/b13/inbox

mkfifo 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:

Terminal window
file="$HOME/lab/b13/work.tmp"
exec 3<> "$HOME/lab/b13/inbox"
trap 'rm "$file"; printf "TERM: removed work.tmp\n"; exit 0' TERM
printf 'temporary data\n' > "$file"
read -r -t 300 -u 3
rm "$file"

This is a supplied worker, not a scripting exercise. Read its responsibilities before running it:

  • file names 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.
  • trap registers the quoted commands as its TERM handler: remove the temporary file, report that removal, and exit successfully.
  • printf writes the temporary data. read waits 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.

Terminal window
bash ~/lab/b13/cooperative.sh &
process=$!
ps -p "$process" -o pid,ppid,comm
ls ~/lab/b13/work.tmp
kill "$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.

Observe cooperative TERM cleanup
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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. The worker exists and has a temporary file; default TERM runs its handler, reports removal, and exits successfully before the absence check.
Steps (17)
  1. Type bash ~/lab/b13/cooperative.sh &
  2. Press Enter
  3. Output: [1] 22
  4. Type process=$!
  5. Press Enter
  6. Type ps -p "$process" -o pid,ppid,comm
  7. Press Enter
  8. Output: PID PPID COMMAND 22 11 bash
  9. Type ls ~/lab/b13/work.tmp
  10. Press Enter
  11. Output: /home/learner/lab/b13/work.tmp
  12. Type kill "$process"; wait "$process"
  13. Press Enter
  14. Output: TERM: removed work.tmp [1]+ Done bash ~/lab/b13/cooperative.sh
  15. Type test ! -e ~/lab/b13/work.tmp && printf 'File absent after TERM\n'
  16. Press Enter
  17. 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 $!:

Terminal window
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?

Terminal window
kill -KILL "$process"; wait "$process"
ps -p "$process" -o pid,comm
cat ~/lab/b13/work.tmp
Run it, then compare with your guess
Force termination and inspect the leftover file
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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. KILL ends the second worker without its TERM message; ps finds no process, but cat can still read temporary data from work.tmp.
Steps (15)
  1. Type bash ~/lab/b13/cooperative.sh & process=$!
  2. Press Enter
  3. Output: [1] 26
  4. Type test -f ~/lab/b13/work.tmp && printf 'File exists before KILL\n'
  5. Press Enter
  6. Output: File exists before KILL
  7. Type kill -KILL "$process"; wait "$process"
  8. Press Enter
  9. Output: [1]+ Killed bash ~/lab/b13/cooperative.sh
  10. Type ps -p "$process" -o pid,comm
  11. Press Enter
  12. Output: PID COMMAND
  13. Type cat ~/lab/b13/work.tmp
  14. Press Enter
  15. 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.

TERM allows cleanup; KILL skips the trap Two separate runs of the cooperative worker. TERM runs its trap and removes work.tmp before exiting. KILL ends it without user-space cleanup. The kernel closes descriptors in both cases; after KILL work.tmp remains. Shell sends TERM Worker runs trap Removes work.tmp Worker exits normally Kernel closes descriptors work.tmp is absent Shell sends KILL Kernel ends worker No trap runs No user-space cleanup Kernel closes descriptors work.tmp remains
TERM allows cleanup; KILL skips the trap Two separate runs of the cooperative worker. TERM runs its trap and removes work.tmp before exiting. KILL ends it without user-space cleanup. The kernel closes descriptors in both cases; after KILL work.tmp remains. Shell sends TERM Worker runs trap Removes work.tmp Worker exits normally Kernel closes descriptors work.tmp is absent Shell sends KILL Kernel ends worker No trap runs No user-space cleanup Kernel closes descriptors work.tmp remains
Simplified model. TERM lets this worker remove work.tmp before exiting; KILL skips its trap. The kernel closes descriptors in both paths, but work.tmp remains after KILL.

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.

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.

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.

The forceful comparison deliberately leaves a file. Remove only this exercise’s paths:

Terminal window
cd ~/lab/b13
rm work.tmp inbox cooperative.sh
cd ..
rmdir ~/lab/b13
unset process

The 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.

Remove the leftover and the waiting fixture
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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. The reset removes the leftover file, named pipe, and script after both worker runs have ended, then removes their empty directory.
Steps (10)
  1. Type cd ~/lab/b13
  2. Press Enter
  3. Type rm work.tmp inbox cooperative.sh
  4. Press Enter
  5. Type cd ..
  6. Press Enter
  7. Type rmdir ~/lab/b13
  8. Press Enter
  9. Type unset process
  10. Press Enter
  • Ctrl+C sends INT to foreground work; a program’s response depends on its signal handling.
  • kill PID requests 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.