What each signal means: TERM, KILL, HUP, STOP and CONT
Video 123 of 140 in Virtualization Foundations, in the unit “Processes, signals, /proc and /sys”. 4:06, with English and Hindi audio.
To stop, pause or reload a running program, you send it a signal. A signal is a short numbered message that the kernel delivers to a process, and the process decides how to react. Five signals cover most of the work: TERM, KILL, HUP, STOP and CONT. Each one asks for something different, and only some of them can be caught or ignored. This article explains each signal, how to send one with kill, and what can go wrong with each.
What you will learn
- A signal is a short numbered message from the kernel, or from a user, to a process.
- TERM asks a program to finish and clean up, and it is the default for kill.
- KILL ends a process at once, and the program cannot catch or ignore it.
- HUP is the hangup signal, and many services read it as a request to reload their settings.
- STOP pauses a process, and CONT lets it carry on from the same point.
A message sent to a running program
Suppose a stock report is running on a warehouse server, and it must stop. You cannot reach into its memory and switch it off. Instead, you send a signal. A signal is a short message that the kernel delivers to a process. It has a name, such as TERM, and a number, such as fifteen.
You send the signal with the kill command, naming the signal and the process ID. The name kill is misleading, because the command can also pause a process or ask it to reload.
How the five signals differ
- TERM, number 15: asks the program to finish and clean up. It is the default for kill, and the one to try first.
- KILL, number 9: ends the process at once. It cannot be caught, ignored or cleaned up after.
- HUP, number 1: the terminal hung up. Many services reload their settings when they receive it.
- STOP, number 19: pauses the process, and it cannot be caught. A paused process keeps its memory, and it uses no CPU while paused.
- CONT, number 18: resumes a stopped process, so it carries on from the same point.
TERM is the polite request, and a program can catch it, save its work and leave. KILL and STOP are the two that a program cannot catch. KILL ends the process, and STOP only pauses it.
Sending signals from the command line
kill -lThe list is long, so start with the first few lines, which show HUP, INT and QUIT. Name the signal after a dash, followed by the process ID. A plain TERM is the default, so kill with only the ID sends TERM. kill prints nothing when the signal is delivered.
kill -TERM 4312kill -HUP 4312Note: HUP asks the program to reload its settings, if it is written to do that.
kill -STOP 4312kill -CONT 4312What goes wrong with signals
The main risk of KILL is that the program never gets to clean up. Files can be left half written, and lock files can stay behind. HUP does not always mean reload. Some programs simply exit when they receive it, so check what the program does before you send it.
A stopped process still holds its memory and its open files. Other work can stay blocked until you continue it or end it.
Try it yourself
Use a process that you own and may stop or restart, and replace 4312 with its process ID. Some programs exit on HUP instead of reloading, so pick one you can start again.
List every signal by name and number. Start with the first few lines.
kill -lWhat you should see: The list starts with HUP, INT and QUIT.
Ask the process to stop politely. kill prints nothing when the signal is delivered.
kill -TERM 4312What you should see: kill prints nothing. A program that handles TERM begins to save its work and exit.
Ask the process to reload its settings. This works only if the program is written to reload on HUP.
kill -HUP 4312What you should see: If the program reloads on HUP, it keeps running and applies its new settings. Otherwise it may exit.
Pause the process. It keeps its memory and uses no CPU while paused.
kill -STOP 4312Let the paused process continue from the same point.
kill -CONT 4312What you should see: The process carries on from the point where it stopped.
Terms
- Signal
- A short numbered message from the kernel, or from a user, to a process. Each signal has a name and a number.
- Hangup (HUP)
- The signal that says a terminal went away, which many services also read as a request to reload their settings.
Check yourself
Which signal can a program catch and use to save its work?
TERM. It asks the program to finish and clean up, and a well-written program can save its work before it leaves.
Why is KILL the risky choice?
KILL ends the process at once, and the program cannot clean up. Files can be left half written, and lock files can stay behind.
What does STOP do, and how is it undone?
STOP pauses the process and keeps its memory. CONT resumes it from the same point.
Previous: Watching live load with top and htop. Next: Killing a process safely.