101

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.

28

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.

  • 42
    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: 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 4:47
  • 49
    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 6 '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 6 '15 at 18:50
179

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

  • 57
    Thanks for posting a quick tutorial and not just linking to offsite man pages! – Toby J Apr 1 '15 at 2:50
  • 2
    systemctl has a cat command shortcut, for reference. – Andy Bettisworth Jan 11 '16 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 '16 at 13:23
  • what are the permissions this file should ideally have? – knocte Dec 22 '16 at 8:49
  • 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 0:09
4

The Redhat documentation is a great source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.