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
| Word | What it means | Example |
|---|---|---|
| Process | a program that is running now | |
| PID | the number that names one process | 4162 |
| Signal | a short message sent to a process | TERM |
| Foreground | the one process that has your keyboard | |
| Background | a process that runs while you type something else | |
| Job | a 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
psShown 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
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
pgrep -f lab-workerShown 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
| Column | Example | Meaning |
|---|---|---|
| UID | student | Who owns the process |
| PID | 4162 | The number that names it |
| PPID | 1 | The PID of its parent |
| STIME | 17:59 | When it started |
| CMD | /bin/bash /usr/local/bin/lab-worker | The 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
| Number | Name | What it does |
|---|---|---|
| 15 | TERM | Asks the process to finish. This is the default |
| 9 | KILL | Removes the process. It cannot be ignored |
| 2 | INT | What Ctrl+C sends |
| 1 | HUP | Often means: read your settings again |
The cells students fill in are left blank in this preview.
Which name is signal 15?
kill -l 15Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
And signal 9?
kill -l 9Shown 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
pkill -f lab-workerShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Is it still there?
pgrep -f lab-workerShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ask the stubborn one
pkill -f lab-stubbornShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Is it still there?
pgrep -f lab-stubbornShown 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
pkill -9 -f lab-stubbornShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
And now?
pgrep -f lab-stubbornShown 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
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
jobsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Stop job 1
kill %1Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
The three busiest processes, once
ps -eo pid,user,%cpu,%mem,cmd --sort=-%cpu | head -n 4Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.