Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Stopping a running program the right way (signals)

After this lesson: I can find a process and stop it the right way.

50 min lesson15 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

The till program that froze

A shop's till program stops answering at the busiest hour. The manager does not want you to restart the whole server. Forty other programs are running on it.

You need to find that one program and stop it, and leave the rest alone.

By the end of this lesson

Note

I can find a process and stop it the right way.

Key words

WordWhat it meansExample
Processa program that is running now
PIDthe number that names one process4162
Signala short message sent to a processTERM
Foregroundthe one process that has your keyboard
Backgrounda process that runs while you type something else
Joba process the shell started and keeps a list of[1]

The cells students fill in are left blank in this preview.

Every running program is a process

When you start a program, the system makes a process for it. The process gets a number, its PID.

A process also has an owner, a start time and a parent: the process that started it. The first process started at boot, systemd on Ubuntu, has the PID 1. Almost every other process descends from it.

The processes in your session

Command
ps

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Every process, then only the lab programs

Command
ps -ef | grep lab-

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Only the PID of the worker

Command
pgrep -f lab-worker

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: one line of ps -ef

ColumnExampleMeaning
UIDstudentWho owns the process
PID4162The number that names it
PPID1The PID of its parent
STIME17:59When it started
CMD/bin/bash /usr/local/bin/lab-workerThe command that is running

The cells students fill in are left blank in this preview.

A signal asks a process to stop

You stop a process by sending it a signal. kill 4162 sends signal 15, called TERM. It asks the process to finish. A good program saves its work and exits.

kill -9 4162 sends signal 9, called KILL. The system removes the process at once. The program never gets a chance to tidy up.

Four signals worth knowing

NumberNameWhat it does
15TERMAsks the process to finish. This is the default
9KILLRemoves the process. It cannot be ignored
2INTWhat Ctrl+C sends
1HUPOften means: read your settings again

The cells students fill in are left blank in this preview.

Which name is signal 15?

Command
kill -l 15

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

And signal 9?

Command
kill -l 9

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Stop the lab's two programs

Typing a PID by hand is slow, and a Run button cannot know yours. pkill -f name finds the process by its name and sends it a signal.

The worker ends on a plain request. The stubborn one ignores it. Watch what pgrep prints after each step. Nothing printed means no such process.

Ask the worker to finish

Command
pkill -f lab-worker

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Is it still there?

Command
pgrep -f lab-worker

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask the stubborn one

Command
pkill -f lab-stubborn

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Is it still there?

Command
pgrep -f lab-stubborn

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Now remove it with signal 9

Command
pkill -9 -f lab-stubborn

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

And now?

Command
pgrep -f lab-stubborn

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think kill -9 is just a stronger kill, so they use it first. kill lets a program save its work and tidy up. kill -9 gives it no chance, and can leave a half-written file. Send TERM, wait a few seconds, and use 9 only when the process will not stop.

Foreground, background and top

A program you start runs in the foreground. It has your keyboard until it ends. Ctrl+C sends it signal 2 and usually ends it.

End a command with & and it runs in the background. The shell lists these as jobs and numbers them: [1].

Type top to watch the busiest processes live. Press q to leave. A Run cannot press q, so type this one yourself.

Start a five minute wait in the background

Command
sleep 300 &

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

List your jobs

Command
jobs

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Stop job 1

Command
kill %1

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The three busiest processes, once

Command
ps -eo pid,user,%cpu,%mem,cmd --sort=-%cpu | head -n 4

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.