Writing systemd service files

Allow systemd and its systemctl command to start, stop, and report the status of a daemon by creating a service file in /etc/systemd/system.

Example: Setup the irqbalance daemon I installed on my Raspberry Pi for systemd control by creating /etc/systemd/system/irqbalance.service:

Description=Daemon to distribute hardware interrupts across multi-CPUs

# man systemd.service: "If set to forking... it is recommended to also use the
# PIDFile= option, so that systemd can identify the main process of the daemon."
ExecStart=/usr/local/sbin/irqbalance --pid=/var/run/irqbalance.pid


Set the new service to be auto-started at boot:

$ sudo systemctl daemon-reload && sudo systemctl enable irqbalance.service
Created symlink from /etc/systemd/system/multi-user.target.wants/irqbalance.service to /etc/systemd/system/irqbalance.service.

Launch daemon and inspect running service:

$ sudo systemctl start irqbalance.service && sudo systemctl status irqbalance

Happy hacking!

Source: Writing systemd unit files

More • linux