Creating a Linux Service with Systemd

2021-04-12

The best way to run something at startup on a linux machine is by creating a service. This process can be finnicky and error prone, so here is a quick guide.

First create a service file. This lives in /etc/systemd/system. If the service is called foo, name the file foo.service.

[Unit]
Description=Foo

[Service]
ExecStart=/path/to/binary with arguments

[Install]
WantedBy=multi-user.target

The WantedBy=multi-user.target line will tell systemd to enable the service once the system has booted.

Now enable the service using

systemctl enable foo.service
systemctl start foo.service

And check the status is enabled and not failed using service foo status. To see logs from your service, run journalctl -u foo.

← Back