[HOWTO] use systemd instead of rc.local on Linux to auto start the server

Today rc.local is more or less obsolete and replaced by systemd in most Linux distribution. As from Ubuntu 16 (I think) rc.local is not enabled by default. You will actually have to run it as a systemd service if you decide to enable it :wink:

So, a brief tutorial coming up!

Open up a terminal window on your system. Root privileges are needed, but if you logon as root or use sudo doesn’t matter.

Units created by sysadm should be placed in /etc/systemd/system/ so we change our directory accordingly:

~# cd /etc/systemd/system

Next we will create a unit that will act as a service (.service). I use nano as text editor and “blynk” seems like an appropriate prefix for the service. So the name of the file we create will be blynk.service.

~# nano blynk.service

Copy/paste this code:

[Unit]
Description=Starts and stops the Blynk server daemon

[Service]
WorkingDirectory=/home/blynk
User=blynk
Group=blynk
Restart=on-failure
RestartSec=10

ExecStart=/usr/bin/java -jar /home/blynk/server-0.29.2.jar -dataFolder /home/blynk

[Install]
WantedBy=multi-user.target

Change WorkingDirectory=, User= and Group= to match your settings. If you run the server as root, just delete the lines with User= and Group=

In ExecStart= you must use absolute paths!

Save and exit!

Stop your running Blynk server and clean up the rc.local and/or crontab.

Make the newly created service start at boot:

~# systemctl preset blynk.service

reboot your system or start it manually with:

~# service blynk start

Other valid options are start | stop | restart | status


Words Of Advice

The above code may or may not work on your system. On my two Ubuntu 16.04.3 LTS it works fine, but that’s the only servers I could test it on. So please backup your current configuration if you intend to make the switch to systemd :slight_smile:

Some links:
Wikipedia - Systemd
Archlinux Wiki
raspberrypi.org



EDIT: One major advantage of using a systemd service instead of rc.local and crontab is that systemd automatically (re)starts the service if the process dies. From my example code above, these two lines tells systemd to 1) Restart the service if the main (ExecStart=) program makes an unclean exit and 2) Do it within 10 seconds.

Restart=on-failure
RestartSec=10

See systemd.service (236) man page for more examples and options.

4 Likes