Processes and signals

INPUT · Slides

Send a signal and stop it (`kill`)

01 / 08

kill does not mean "kill"

The name is grim, and all kill does is this.

> send one signal to a process

The signal is called, well, a signal. Like posting a single letter through the door.

What the process does on receiving it is up to the process. It may tidy up and finish, or it may ignore it.

~ $ kill 84

That is "number 84, would you finish, please?". Not an order.

It is called kill because the default signal usually ends in the process finishing. Take it as a trick of history. All you send is a signal — the most important thing today.

02 / 08

Signals have names and numbers

You can list them with kill -l. There are 33.

~ $ kill -l 1) HUP 2) INT 3) QUIT ... 9) KILL ...15) TERM

You only need four.

NumberNameMeaning
15TERM"tidy up and finish" (the default)
9KILLremoved, no discussion
2INTthe one Ctrl-C sends
1HUPused for "reread your settings"

There are three ways to write it, all the same.

kill 84         the default (TERM)kill -15 84     by numberkill -TERM 84   by name

By name reads better, and -9 in particular is mostly used as a number. "Send it a nine" is understood, it is that famous.

03 / 08

TERM is a request

The default TERM leaves the receiving process room to think.

A well-made program, on receiving TERM, does this.

1. saves the file it was part way through
2. closes what it had open
3. clears away its temporary files
4. and then finishes

That clearing away is called cleaning up. Which is why sending TERM first is the etiquette.

kill 84        <- this first

And if you remove it with no room to clear up? A file half written, temporary files scattered about, and with a database, real damage.

The more of a hurry you are in, the more you start with TERM. That is basic etiquette on a real server.

04 / 08

KILL is the last resort

Some programs ignore TERM. Some are spinning endlessly with no chance to read the letter.

The last resort then is -9 (KILL).

kill -9 84

This never reaches the process. The kernel removes it directly, and the receiving side can do nothing.

TERM  -> reaches the process -> it does as it likesKILL  -> the kernel removes it -> no resisting

So it certainly goes. And not one bit of cleaning up runs.

Learn the routine.

1. kill PID          ask2. wait a few seconds and check3. if still alive, kill -9 PID

Plenty of people are in the habit of typing -9 first, and that is like pulling the plug out every time you shut down. Usually fine, until the day it breaks.

05 / 08

Three ways to point at the target

There are three ways to write who you are sending to.

kill 84                 by numberkill %1                 by job numberkillall sleep           by name, all of them

To find the number, last lesson's tools work.

kill $(pidof yes)       look the number up from a name and pass it

$( ) was "run the inside and put the result here".

killall is handy, and be careful. It stops everything with that name, and can take things nothing to do with you.

There is one more, for checking that something is alive without sending anything.

kill -0 84

-0 means "signal number 0", that is, send nothing, and what comes back is only whether the target is there. 0 if alive, 1 if not. A common shape in checking scripts.

06 / 08

Ctrl-C was a signal too

You have already sent a signal. Ctrl-C.

~ $ sleep 30(nothing comes back)^C~ $

What gets sent there is INT (number 2), the signal for "interrupting".

Here is the mechanism. Press Ctrl-C and the terminal sends INT to whatever is running in front. So it is the same as typing kill -2 number.

There are other signals you can send from keys.

KeySignalMeaning
Ctrl-CINTplease stop
Ctrl-ZTSTPpause for now (next lesson)
Ctrl-\QUITstop, and leave a record

You can see what was going on behind the button. No magic, just a numbered signal flying across.

07 / 08

The number tells you how it stopped

A process finished by a signal leaves how it went in the exit status.

~ $ timeout 2 sleep 10Terminated~ $ echo $?143

The number 143 means something: 128 + 15, and 15 is TERM's number.

128 + the signal number = the exit status128 + 15(TERM) = 143128 +  9(KILL) = 137128 +  2(INT)  = 130

Those three you see a lot in the field. Find 137 in a log and you can read it as somebody (or the kernel) removed it with -9. The kernel removing something for want of memory also gives 137, so it is a clue when hunting for a cause.

timeout seconds command is the tool for "cut it off after a set time". Inside, it sends TERM when the time comes. Add -s KILL and it sends KILL, giving 137.

Exit statuses get proper treatment in chapter 11. Here, just remember that the number tells you the cause of death.

08 / 08

Now have a go

These are the tools.

kill number       send TERM (a request)kill -9 number    send KILL (force)kill -0 number    only see whether it is alivekill %1           point by job numberkill -l           list the signalskillall name      stop everything with that nametimeout secs ...  cut off after a time

There is a stubborn.sh in your home, a script that catches TERM and does not finish. It lets you meet a target that will not listen to a request.

Inside, it is this.

trap "echo not listening" TERMwhile true; do sleep 1; done

trap says "when TERM arrives, do this". The writing comes in chapter 11, so you need not read it yet.

Only stop harmless things like sleep and yes. kill 1 is protected here so it does nothing, and on a real server the machine stops.