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 program and starting a service

After this lesson: I can find and stop a program, and start a service.

45 min lesson10 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

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

WordWhat it meansExample
Processa program that is running now
PIDthe number that names one process1507
Signala short message sent to a processTERM
Servicea program the machine keeps running in the background
systemdthe program that starts and watches services
systemctlthe 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

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 PIDs and names

Command
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

ColumnExample from a tillWhat it tells you
UIDtillWho runs it
PID1203Its number
PPID1The number of the process that started it
STIME09:01When it started
CMD/usr/bin/python3 /opt/till/till.pyThe 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

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 the worker 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 force 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.

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

Code: bash
# /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?

Command
systemctl is-active lab-worker

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

Start the worker again

Command
sudo systemctl start lab-worker

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

Ask again

Command
systemctl is-active lab-worker

Shown 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-.

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.