Linux Startup Scripts with SystemD

August 26, 2017

With SysVinit it was quite time consuming and error-prone to build a startup script. Due to the limit scope of this init system it wasn't able to resolve startup dependencies or to restart application when they crash. systemd is able to address all these requirements and makes it pretty simple to create a startup script. The following example of the Ghost CMS shows how to do:

[Unit]
Description=Ghost CMS

[Service]
Type=simple
User=ghost
ExecStart=/usr/local/bin/npm start
WorkingDirectory=/opt/ghost
RestartSec=5s
Restart=always

[Install]
WantedBy=multi-user.target

A description of all available properties can be found here:
https://www.freedesktop.org/software/systemd/man/systemd.unit.html

Store the script inside systemd directory /etc/systemd/system and execute the following commands to enable the script:

systemctl daemon-reload
systemctl enable ghost.service

Note: Replace ghost.service with the filename which you've chosen inside the systemd directory.

To check the status of the service exeute systemctl status ghost.service or journalctl -u ghost.service to see the full log.