Writing basic systemd service files

I am developing a Nodejs application that the user interacts with via HTTP on localhost. There are practically no parameters and the daemon has virtually no dependencies and it just needs to be up by log-in time.

I would like to follow the idioms on each platform for start-up scripts, and that means Upstart on Ubuntu and systemd on Fedora.

  • Are there any good tutorials for writing systemd system files?
  • Are there any ‘best practices’ to be aware of?

I have found these resources:

I am mostly looking for an API of sorts as a reference, as well as a basic format to follow.

Asked By: beatgammit


I would start with the Systemd manual pages. They represent a comprehensive resource of the system and services.

There is also the freedesktop Systemd FAQs.

Answered By: jasonwryan

The following example is inspired by this link, which actually does not mention all steps and is listed just to credit the source: http://patrakov.blogspot.com/2011/01/writing-systemd-service-files.html

Step 1: I created this file (note location) which essentially fires a bash process with an extended argument. You could fire your own command which could be different from bash.

[root@y500-fedora ~]# cat /etc/systemd/system/foo.service 

ExecStart=/bin/bash -c "while true; do /bin/inotifywait -qq --event close_write /sys/class/backlight/acpi_video0/brightness; su myusername -c '/bin/xbacklight -display :0 -set $(cat /sys/class/backlight/acpi_video0/brightness)'; done"


Step 2:

Reload systemd:

systemctl daemon-reload

Start the new service:

systemctl enable foo

(similarly you can disable it)

(optional) Step 3: It should start automatically at next reboot into multi-user mode (run level 3) but if you want to start it right away:

systemctl start foo
systemctl status foo # optional, just to verify

Update: For completeness, I should add that ubuntu bionic seems to have a very thorough man page. RTFM here

Answered By: necromancer

The Arch wiki’s page on writing systemd units is quite comprehensive and accessible.

Answered By: berinaniesh
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.