118

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.

2

4 Answers 4

193

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 
[Unit]
Description=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"

[Install]
WantedBy=multi-user.target

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

5
  • 2
    systemctl has a cat command shortcut, for reference.
    – wurde
    Jan 11, 2016 at 0:04
  • What is the cat command shortcut? Like a way to create and edit the systemd control files without entering the whole path?
    – JpaytonWPD
    Feb 1, 2016 at 13:23
  • what are the permissions this file should ideally have?
    – knocte
    Dec 22, 2016 at 8:49
  • @knocte the permissions are moot, since it will be a privileged process that will be reading it and just reading it not writing. the location of the file is a privileged location (/usr/lib/systemd/...) and you will likely need some special permissions to add your file there. Jan 14, 2017 at 21:45
  • 2
    According to systemd.unit man page, you should put it in /etc/systemd/system. /usr/lib/systemd is where the proper distribution packages put their files.
    – wbkang
    May 29, 2017 at 0:09
27

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.

10
  • 48
    The man pages will be useful (I wish it was cleaner though), but is there a basic system file tutorial? Like which parts are necessary, which parts are recommended, and which parts are optional. The man pages seem to assume that I'm already an expert in systemd system files, which I am not.
    – beatgammit
    Jun 21, 2011 at 15:54
  • Not that I am aware of, unfortunately. There is probably an assumption that if you are writing initscripts, you already know how to write initscripts. Look through the Arch scripts and pick one that is similar to what you need: archlinux.org/packages/community/i686/systemd
    – jasonwryan
    Jun 21, 2011 at 18:28
  • 1
    Thanks! With your help and the blog I found, I was able to get a basic one working. Systemd seems a lot nicer than upstart or the old systemv init daemon.
    – beatgammit
    Jun 22, 2011 at 4:47
  • 61
    Who has time in there lives to START with that. I mean, if you know what you are doing that is a great reference, but holy cow the manual pages are complex. Apr 6, 2015 at 16:51
  • 1
    @macmadness86 The OP was looking for an authoritative source (see the last para); for most needs, reading the service file for a similar app and then consulting specific parts of the man page will be more than enough to get you there. They are actually pretty straightforward.
    – jasonwryan
    Apr 6, 2015 at 18:50
6

The Redhat documentation is a great source.

5
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .