Signals and kill

Beginner · 5 min

Signals are how you ask a process to stop, reload or pause. Learn kill, SIGTERM versus SIGKILL, the 128+n exit codes, and pkill. Numbers differ on macOS.

Before thisProcesses: ps, top and the process treeExit status and && / || listsTraps and cleanup

You Suck at Programming · 5 min

Signals and kill

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by You Suck at ProgrammingReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A signal is a small numbered message the kernel delivers to a process. kill is badly named: it sends any signal, and the default is SIGTERM, a polite request to stop.
  • SIGTERM can be caught or ignored, so a process can clean up. SIGKILL (9) and SIGSTOP cannot be caught, and the kernel acts immediately with no cleanup.
  • Try kill PID first and wait a moment. Reach for kill -9 only when the process ignores SIGTERM, because it skips temp-file cleanup and child shutdown.
  • A process killed by signal n reports exit status 128+n, so 143 means SIGTERM, 137 SIGKILL and 130 Ctrl-C. Prefer names over numbers: the numbers for STOP, CONT and USR1 differ between macOS and Linux.
  • kill only signals the PID you name. A parent's children are not stopped with it. pkill matches by name and kill -0 PID tests that a process exists.

Why processes need a way to be interrupted

A process is a program running on its own. It will not stop to read your keyboard unless it chooses to. Yet you need some way to say “stop”, “reload your config” or “pause” without the program polling for it.

The kernel’s answer is the signal: a numbered message delivered out of band. When one arrives, the process runs its handler for that number. If it has none, the default applies, which is often “terminate”. You already use signals. Ctrl-C asks the terminal to send SIGINT to the foreground group.

kill sends signals, not only deaths

kill is the command that sends a signal to a PID. The name is historical. Bash has it as a builtin, so kill -l lists the names:

$ kill -l | head -3
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGEMT	 8) SIGFPE	 9) SIGKILL	10) SIGBUS
11) SIGSEGV	12) SIGSYS	13) SIGPIPE	14) SIGALRM	15) SIGTERM

The ones you need: HUP (1, the terminal went away; many daemons reload config on it), INT (2, Ctrl-C), TERM (15, please stop), KILL (9, stop now), and STOP/CONT to pause and resume.

I ran the rest on macOS with Bash 5.3. Names transfer between systems, but numbers do not:

$ echo "TERM=$(kill -l TERM) KILL=$(kill -l KILL) STOP=$(kill -l STOP) CONT=$(kill -l CONT) USR1=$(kill -l USR1)"
TERM=15 KILL=9 STOP=17 CONT=19 USR1=30

On Linux, STOP is 19, CONT is 18 and USR1 is 10, from the Linux documentation, which I did not run. So kill -19 pauses a process on Linux and resumes one on macOS. Write kill -STOP.

Exit codes carry the signal

When a signal kills a process, the shell reports 128 plus the signal number, which is why exit status 143 shows up in logs:

$ sleep 100 & p=$!
$ kill "$p"; wait "$p"; echo "exit status: $?"
exit status: 143
$ sleep 100 & p=$!
$ kill -KILL "$p"; wait "$p"; echo "exit status after KILL: $?"
exit status after KILL: 137
$ sleep 100 & p=$!
$ kill -INT "$p"; wait "$p"; echo "INT status: $?"
INT status: 130

Bash also prints Killed: 9 for the KILL case, which is noise but confirms the cause.

TERM versus KILL

I wrote a script with handlers:

#!/usr/bin/env bash
trap 'echo "got TERM, cleaning up"; exit 0' TERM
trap 'echo "got HUP, reloading config"' HUP
echo "ready $$"
while true; do sleep 0.2; done

Started in the background, then kill -HUP and a plain kill:

ready 15785
got HUP, reloading config
got TERM, cleaning up

Exit status 0, because the handler exited cleanly. That is why TERM is the first thing to send: the program gets to remove its temp files and stop its children (the traps lesson covers writing the handler).

A second script with trap '' TERM ignores the request:

$ kill "$s"; sleep 0.5; kill -0 "$s" && echo "still alive after TERM"
still alive after TERM
$ kill -9 "$s"; wait "$s" 2>/dev/null; echo "status after KILL: $?"
status after KILL: 137

kill -0 sends nothing and just reports whether the PID exists and is yours to signal. KILL cannot be caught or ignored, because the kernel does the killing, not the process. The price is no cleanup, so reaching for -9 first leaves lock files and half-written data behind.

Pause, resume and permissions

STOP and CONT are the same mechanism that Ctrl-Z uses:

$ kill -STOP "$p"; ps -o pid=,stat= -p "$p"
15856 T
$ kill -CONT "$p"; ps -o pid=,stat= -p "$p"
15856 S

You can only signal your own processes, and a missing PID is an error:

$ kill 1
kill: (1) - Operation not permitted
$ kill -0 999999
kill: (999999) - No such process

Signals go to one PID

I started bash -c 'sleep 77 & sleep 78 & wait', then killed that bash. Both sleeps stayed running. Signalling a parent does not signal its children, and they are re-parented to PID 1.

To act on names, pgrep -fl 'sleep 211' lists matches and pkill -f 'sleep 211' signals them. Both worked here, and both exist on Linux too. Always preview with pgrep before pkill, because -f matches the whole command line.

About the video

“Basic Unix signals with kill - SIGTERM, SIGINT, SIGKILL, etc.” is from You Suck at Programming (Dave Eddy), published in August 2024. It runs 4 minutes 30 seconds. The description has no chapters and I could not read a transcript, so I am relying on the title. The channel is English-language. The video may use Linux or macOS numbers, so keep to names as above.

Where this fits

You can now start, inspect and stop processes by PID. Next, the shell’s own bookkeeping for processes you started: job control.

Check yourself

  1. 01A script exits with status 143. What happened?

    It was terminated by signal 15, SIGTERM, because shells report 128 plus the signal number. 137 would be SIGKILL and 130 SIGINT.

  2. 02Why try kill PID before kill -9 PID?

    SIGTERM lets a program catch it, remove temp files, flush buffers and stop its children. SIGKILL cannot be caught, so none of that cleanup runs.

  3. 03You add trap '' TERM to a script. Does kill PID stop it? Does kill -9 PID?

    kill PID does not, because the empty trap ignores SIGTERM. kill -9 does, because SIGKILL cannot be trapped or ignored.

  4. 04You kill a Bash script that started two background sleeps. Are the sleeps still running?

    Yes. kill sends a signal to one PID only, and the children are re-parented to PID 1. Signal the process group or use pkill on their names.

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark