How to create and manage Systemd Services

Most Linux distributions have switched over to SystemD for managing processes. This tutorial will attempt to increase your understanding of it and show you how to manage processes with it.

You’ve just compiled some software you want to run as a service, but don’t know how. This tutorial will show you how easy it can be to write your own service files for Systemd.

Registering Services

Services are registered by creating .service files under /etc/systemd/system. For example, a service file for Apache2 would be named apache2.service.

The Service file

The following is an example of a SystemD service file. Unit describes the service.

[Unit]
Description=My Miscellaneous Service
After=network.target

[Service]
Type=simple
User=nanodano
WorkingDirectory=/home/nanodano
ExecStart=/home/nanodano/my_daemon --option=123
Restart=on-failure # or always, on-abort, etc

[Install]
WantedBy=multi-user.target

Managing Your Service

Daemon Reload

Whenever you make modifications to a SystemD service or unit file you will need to instruct SystemD to reload it. These are the files located under /etc/systemd/system directory and not the configuration file of the service itself.

To reload the SystemD configuration file, use the following command

sudo systemctl daemon-reload

Start a Service

sudo systemctl start <service name>

Stop a Service

sudo systemctl stop <service name>

Reload a Service

Service reloads attempt to minimize outages when configuration changes need to be applied.

sudo systemctl reload <service name>