Creating a cronjob to auto check if service node is running.
Firstly, I would like to reiterate the importance of chosing a high quality reliable VPS provider. Running the server daemon off of a home computer, or any computer / network that is not 99.9% stable presents problems. We highly discourage you from running the server daemon on an unreliable computer – If your home computer turns off, disconnects, reboots, etc, most likely your XRN will be locked for the remainder of your staking period. (< 30 days) If you are reading this and have the server daemon on a home pc, stop right now. Go purchase a reliable VPS and redeploy the server daemon.
Quick Notes before starting.
My VPS is running: Linux 4.16.14-feral x86_64
Use terminal command “pwd” to determine direct path.
Use terminal command “ps x” to show running procs, to kill a process type “kill” and then it’s process ID “kill 33333”.
Use terminal command CTRL + X to save nano files, or other information.
PS; Through this guide I will be replacing certain pieces of information for security purposes, typically my username for my server.
What needs to happen.
Here’s what needs to happen in order for this automated process:
1:) You must create a script to run screen + saronited —service-node 2:) You must create a “job” to run to check to see if “saronited” is running. “saronited” is the identifer we are going to attach to the “screen” and it will be an automated process which will run the server daemon; AKA “saronited –service-node” AKA the important part of your server 🙂
In this example, I am using the VPS provider FeralHosting.com – some variables may be different according to your vps host. I have had 100% uptime with FeralHosting, just saying.
PART 1 – SCRIPT CREATION
Firstly, log into your VPS and type “ls” this will print your current (typically home) directory. The name of the script we’re going to create is “saroscript” (our script we’re going to make for our autoamted cron job) in the base directory (( AKA ~/ (short key to get to your base dir is “cd ~/” ))
Once in a directory where you can create files, lets say your home directory, type “nano saroscript.sh” (You may be prompted to select a text editor, I use Nano)
Once inside your new “saroscript.sh” get ready to paste the text below. BUT FIRST, you need to correct the location to the full path. This information can be obtained by typing from the terminal command “pwd”. My example is on Pastebin at the link below, you will have to adjust it accordingly.
Example is here: https://pastebin.com/TDmukUPr
Once you’ve made the nesscary changes hold CTRL + X -then hit “y” to save your nano document.
After saving your new script, run this command in the directory where you created the script: chmod 700 ~/saroscript.sh
This script is basically first a check (which is indicated by [[ ]] – It’s say “Check if “saronited” is running, if no then run the next line : screen -dmS saronited — /media/md1/YOURUSERNAMEHERE/saronite-linux-64-v18.104.22.168/saronited –service-node
The script above firstly runs “screen” (as you learned from the node setup guide screen creates a detached screen that will run in the background.) The script then gives the “screen” a name of “saronited” and then it calls this path /media/md1/YOURUSERNAMEHERE/saronite-linux-64-v22.214.171.124/saronited –service-node (which if everything works, should turn on your service node)
Before moving forward, I would test this script by simply typing “./saroscript.sh” in the directory it’s located. For our example the dir it will run out of it is your home dir (~/ ) “/media/md1/YOURHOMEDIRECTORY” – – – Assuming your service node daemon (saronited –service-node) it not already running, it should start in a detached screen.
If it’s already running, you will get something like
++ pgrep -fu YOURUSERNAME saronited
+ [[ -n 38333 ]]”
Which means the script found “saronited” (or a similar process) already running as process ID 38333.
(optional) To list all detached screens type “screen -ls”.
To switch to a running an already running “screen” type screen -x 36234 (<- this would be your PID number, not necessarily 36234
If you want to kill a screen process, type “ps x” then “kill yourprocessid here”.
PART 2 – CRONJOB CREATION
OKAY! Now you have created the script, and you know how to move between screens and understand the context of the situation lets move to creating the cronjob.
In any area in linux, type “crontab -e“
You should be presented with a window with some marked out content indicated by ### symbols. Ignore these.
Arrow down to an area with no # symbols.
Get ready to copy and paste this, but before you do, you need to adjust it accordingly.
Example is between dashed lines. Pastebin here: https://pastebin.com/7hM7beBP
*/5 * * * * /media/md1/YOURUSERNAME/saroscript.sh >/media/md1/YOURUSERNAME/saroscript.sh.cronoutput 2>&1
Remember what we did earlier? Correcting the direct path? We have to do that again. Remember, command “PWD” gives you the full path of your current terminal location. Also recall that I created my “saroscript.sh” in my home directory of “media/md1/YOURUSERNAME” – – – Once everything is correct, hold CTRL + X then hit “y” to save your cronjob.
The above cronjob runs our “saroscript.sh” every 5 minutes. If the script detects that a process or screen is already running with the name “saronited” you will get a message like so:
++ pgrep -fu YOURUSERNAME saronited
+ [[ -n 38333 ]]”
If you would like to see the most recent log output of your cronjob, simply browse to the location your created your “saroscript” probably in your home dir “cd ~/”, type “ls” and you’ll see “saroscript.sh.cronoutput” – type “nano saroscript.sh.cronoutput” to see log.
If the server reboots, or loses power, the cronjob will automatically start within 5 minutes of the server booting up, it will check if screen / process “saronited” is running, when it detects it isn’t it will create a new screen and run the script “saroscript.sh” which will then run this command “/media/md1/YOURUSERNAMEHERE/saronite-linux-64-v126.96.36.199/saronited –service-node” which will start your service node 🙂