Systemd Crash Course

I originally wrote this for a friend that was switching their homeserver over from Windows to Linux and needed help keeping a Minecraft server alive. I hope it can help others!

Systemd is the Linux init system. It includes a bunch of rules on services/devices/other resources that the system needs to run in order to function. It also includes complex dependency management so that you can set up services to only start when pre-requisites are done.

It’s big and complex, so here’s the bare minimum you need to know to get a service running.

NOTE: Most .deb files will include the relevant systemd files and will install/uninstall themselves as you install/uninstall the deb, so you probably don’t need to do it for these ones. But that’s not universal so some debs may not have them either! Up to the maintainer.

Unit File

You need a file to describe your systemd “unit”. Here’s an example service: /home/tnelson/epaper/epaper.service

[Unit]
Description = "epaper updater"

[Service]
Type=simple
Restart=always
WorkingDirectory=/home/tnelson/epaper
ExecStart=/home/tnelson/epaper/run.py run
User=tnelson

[Install]
WantedBy=network-online.target

Important directives:

Restart - set to Always to have systemd restart the service whenever it dies

ExecStart - this is the actual command to run

User/WorkingDirectory - other configuration that might be needed depending on your service.

WantedBy - this tells systemd when it needs to start this service. The most common ones you’ll use are multi-user.target, which is when the OS is done booting, and network-online.target, which is when the computer has network connectivity (note this is not internet connectivity, this is “hey I got an IP address from the network”)

Full list of service directives can be found in the man pages, which are mirrored here: https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html

Installing a Unit File

Easiest way is to drop it into /etc/systemd/system/

You then need to tell systemd to check for new files by running

sudo systemctl daemon-reload

Then enable the service:

sudo systemctl enable epaper.service

If you used the above configuration, this will not result in the process being started since the network-online.target event happened a while ago.

You just need to start it:

sudo systemctl start epaper.service

Debugging

Helpful CLI Commands

# Get status
sudo systemctl status epaper.service
# Stop running process
sudo systemctl stop epaper.service 
# Stop if running, then start
sudo systemctl restart epaper.service
# Stop systemd from automatically starting service (will not stop the running one though, use stop for that)
sudo systemctl disable epaper.service
# list of units systemd knows about
sudo systemctl list-units

Logs

Systemd stores logs in a compressed binary format that requires a special tool to read. This has pissed many people off who like to hang out in /var/log for everything.

# Show all logs for systemd service
sudo journalctl -u epaper.service
# Follow the log, displaying new entries as they appear
sudo journalctl -f -u epaper.service