Linux Labs

Users, permissions, and processes / Stage 12 of 13

Signals and process termination

Not running Starting usually ~20 s
Leave the lesson

Step 2 of 9 · theory · 5 min

A request and an order

Both commands stopped an export, but one let it finish its work and the other cut it off. This step covers how they differ.

kill sends a process a signal, a short message with a number and a name. For most signals the program decides what to do; for a few, the kernel does:

Signal What it does
TERM (15) asks the process to exit; kill sends it with no option
INT (2) interrupts; Ctrl+C sends it
KILL (9) the kernel ends the process at once, and the program gets no chance to react

A program can answer TERM its own way: write out its data, close its files, and exit. Export a did just that: it appended saved and exited, and the shell reported Done. A program never sees KILL: the kernel cuts the process off wherever the signal finds it, and the shell reports Killed.

You name a signal by name or number: kill -9, kill -KILL, and kill -s KILL are the same thing. kill -l prints the whole list.

Check yourself 1 question

kill 2345 ran without an error, but five seconds later ps -p 2345 still shows the process. What do you do?

  • A second TERM is the same request: the program already knows it is asked to exit. Check ps -p 2345 again in a few seconds.

  • A reboot cuts off every program on the machine. First let the process finish and check ps -p 2345 again.

  • TERM is a request: the program may still be writing its data. kill -9 would cut it off mid-write, like export b.

On TERM a program may take a while to exit, writing out data and closing files. KILL is kept for when the process never exits, and anything unfinished is then lost.

Key points

  • kill with no option sends TERM, a request to exit that lets the program save its work.
  • kill -9 (KILL) ends the process at once, and anything unfinished is lost.
  • So TERM comes first, and KILL is for when the process does not exit on its own.
Ubuntu 24.04
Loading…