The order screen that froze
A takeaway shop shows new orders on a screen in the kitchen. On a busy Friday the screen freezes.
Restarting the whole computer would also stop the card reader and the printer. You need to stop only the frozen program, then start it again.
By the end of this lesson
Note
I can find and stop a program, and start a service.
Key words
| Word | What it means | Example |
|---|---|---|
| Process | a program that is running now | |
| PID | the number that names one process | 1507 |
| Signal | a short message sent to a process | TERM |
| Service | a program the machine keeps running in the background | |
| systemd | the program that starts and watches services | |
| systemctl | the command you use to talk to systemd |
The cells students fill in are left blank in this preview.
Before you start
Note
Press Start the lab on the bar at the top and wait for the machine. This lab runs two small programs for you to stop, lab-worker and lab-stubborn. Press the Runs in order.
Every running program is a process
When a program starts, the machine makes a process for it. Each process gets a number, its PID, and an owner.
ps -ef lists every process. A pipe into grep keeps only the lines with some words in them. pgrep -fl does both jobs at once: it prints the PID and the name of each match.
Every process with lab- in it
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 PIDs and names
pgrep -fl lab-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
| Column | Example from a till | What it tells you |
|---|---|---|
| UID | till | Who runs it |
| PID | 1203 | Its number |
| PPID | 1 | The number of the process that started it |
| STIME | 09:01 | When it started |
| CMD | /usr/bin/python3 /opt/till/till.py | The command that is running |
The cells students fill in are left blank in this preview.
kill asks, kill -9 forces
You stop a process by sending it a signal. kill takes a PID. pkill -f takes a name instead.
The usual signal is TERM. It asks the program to finish, so it can save its work first. A program may ignore TERM. Signal 9, KILL, cannot be ignored: the machine removes the program at once.
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 the worker 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 force 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.
A common mistake
Warning
Many people think kill -9 is just a stronger kill and use it first. Signal 9 gives the program no time to save or tidy up, so it can leave half-written files. Send a plain kill first, wait a few seconds, and use -9 only if it will not stop.
systemd starts services
Both lab programs are services. systemd started them when the machine booted. A small text file, a unit file, tells systemd what to run.
systemctl is-active asks whether a service is running. sudo systemctl start starts it now. sudo systemctl enable makes it start at every boot.
A unit file for another program
# /etc/systemd/system/order-screen.service
[Unit]
Description=Order screen for the kitchen
[Service]
ExecStart=/usr/local/bin/order-screen
Restart=on-failure
[Install]
WantedBy=multi-user.target
Is the worker service running?
systemctl is-active lab-workerShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Start the worker again
sudo systemctl start lab-workerShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ask again
systemctl is-active lab-workerShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Going further
Bring lab-stubborn back the same way you brought the worker back.
First run systemctl is-active lab-stubborn and read the word it prints. It is not inactive this time. Why might signal 9 count as a failure? Then start the service and check it with pgrep -fl lab-.