How do I set my scripts that run my nodes as systemd services?

For a long while many operators using the Guild Operators scripts to run their nodes would have to manually restart their nodes if, for any reason, their node failed.

Last August 2020, after a shout-out from Samuel Leathers, the lead on NixOS operations for the Cardano community at IOHK, it was recommended that operators do run their node restart services as a systemd service.

In this guide, we look at how we go about performing this service.

Credit goes to Damjan Ostrelich who along with his instruction video and his scripts on this subject matter we base most of this guide upon.


Instructions

Log into your server and find your way to your systemd folder (we are using Ubuntu 20.4). But it should be similar on other versions of Unix.

cd /etc/systemd/system

Then lets download Damjan Otrelich's cnode-restart.service script:

wget https://raw.githubusercontent.com/DamjanOstrelic/shelley-scripts/master/cnode-restart.service

Let's check we got the script in our directory:

ls | grep restart

If it finds a script with the name cnode-restart.service you have successfully downloaded the script.

nano cnode-restart.service

We get the following contents

[Unit]
Description=Restarts the cardano-node
After=network-online.target
StartLimitIntervalSec=0

[Service]
Type=forking
Restart=always
RuntimeMaxSec=15 #edit restart time in seconds
User=root
ExecStart=/opt/cardano/cnode/scripts/cnode-restart.sh

[Install]
WantedBy=multi-user.target

Let's edit line 9 of the above script:

RuntimeMaxSec=86400

This will restart the service every 24 hours (86400 seconds). Save the script and you shoudl be good to go. 

However, if you want to test the script against the server then you could do as Damjan did in his video and set RuntimeMaxSec=15 or RuntimeMaxSec=60. If everything works as it should under that edited parameter then re-edit the script back to RuntimeMaxSec=86400 or in Damjan's example RuntimeMaxSec=43200 after you have completed the tests.

For the server to be able to restart the script you need systemctl to reload all daemons:

systemctl daemon-reload

Let's start the cnode-restart.service file

systemctl start cnode-restart.service