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.

beatgammit
  • 7,583

4 Answers4

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

necromancer
  • 2,109
  • 2
    systemctl has a cat command shortcut, for reference. – wurde Jan 11 '16 at 00: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 01 '16 at 13:23
  • what are the permissions this file should ideally have? – knocte Dec 22 '16 at 08: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. – necromancer Jan 14 '17 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 '17 at 00: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.

jasonwryan
  • 73,126
  • 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 '11 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: http://www.archlinux.org/packages/community/i686/systemd/ – jasonwryan Jun 21 '11 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 '11 at 04:47
  • @tjameson, that is what man pages are for... – vonbrand Feb 03 '14 at 02:31
  • 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. – Jonathan Komar Apr 06 '15 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 06 '15 at 18:50
  • @jasonwryan I know this is 5 years later, but, errr, the link you posted is broken. If I find where it is now, I'll edit this comment with it. – johnildergleidisson Dec 16 '16 at 14:22
  • @JoaoMilasch Neither looks broken to me. Which one are you referring to? – jasonwryan Dec 16 '16 at 20:17
  • @jasonwryan: Gosh, sorry for not being specific enough. I meant the link in your comment: archlinux.org/packages/community/i686/systemd which is not in itself that important but I tried to follow it to find examples... – johnildergleidisson Dec 19 '16 at 16:54
  • @JoaoMilasch Oh, yeah: systemd has moved to core: https://www.archlinux.org/packages/core/x86_64/systemd/ – jasonwryan Dec 19 '16 at 16:59
6

The Redhat documentation is a great source.

caracal
  • 161
0

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