CodeTheRobot

Ubuntu Linux for Robotics · Lesson 12 of 96

Systemd services

Units, systemctl, journalctl and writing a service that starts your robot at boot.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain systemd services in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

systemd starts and supervises the services of a Linux system. A unit file describes one service: what to run (ExecStart), as which user, when to start (After=), and whether to restart it on failure. systemctl controls units (start, stop, enable at boot, status) and journalctl reads their logs.

Your robot software should run as a service so it starts on boot and restarts after a crash. Put unit files in /etc/systemd/system, run sudo systemctl daemon-reload after changing them, and read output with journalctl -u name -f. Services do not load your .bashrc, so source ROS explicitly inside the command.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the systemd services commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

systemctl status ssh
# Show whether the ssh service is running.
systemctl --failed
# List services that failed to start.
sudo systemctl enable --now robot-bringup.service
# Start a service now and at every boot.
sudo systemctl restart robot-bringup.service
# Restart it after changing its unit file or code.
journalctl -u robot-bringup.service -f
# Follow its log live (Ctrl+C to stop).
journalctl -b -p err
# Show error messages from the current boot.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · The robot's computer

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

robot computercpumemdiskdriverstoppedplannerstoppedloggerstoppedsafetystoppedTHE ROBOT'S COMPUTER

Demo for Systemd services. Expected result: Active: active (running)

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# /etc/systemd/system/hello.service contains: [Service] ExecStart=/usr/bin/sleep infinity
# TODO: reload systemd, then enable and start hello.service in one command
systemctl status hello.service

Expected output

Active: active (running)
Stuck? Show the full solution (try it yourself first)
# /etc/systemd/system/hello.service contains: [Service] ExecStart=/usr/bin/sleep infinity
sudo systemctl daemon-reload
sudo systemctl enable --now hello.service
systemctl status hello.service

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Write a unit file that runs a small script, enable it, reboot, confirm it started, kill its process to test Restart=on-failure, and read its output with journalctl.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.