The Carv Node Checker script works very similarly to the one for Aethir, which you can find here. Since the whole setup was explained in detail there, this post will only be a short version. So if you face any difficulties understanding the process, read through the guide for the Aethir Node Checker script.
What the Carv Node Checker script is automatically doing:
- It restarts the node if the screen session is gone for some reason
- It restarts the node after the VPS reboots
- It restarts the node if the CLI throws an error (this only works if a log file called CarvScreenLog was created during the screen creation)
- It deletes the CarvScreenLog file if it reaches 100MB
Things to know:
- There must only be one Carv folder present in the home directory (it can be called anything but Carv should be part of the name)
- The script creates a new screen session, which is called Carv
- It creates a log file called CarvScreenLog next to the node binary file
The Script
First, create a new shell file:
cd && nano CarvNodeChecker.sh
Copy the script and paste it into the file:
#! /bin/bash
#
# Carv Node Checker v1.5
# by ciasis for MetaMuffin
# ------------------------
#
# Disclaimer:
# The script should be used at one's own risk.
# I don't take responsibility for any losses.
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# >>>>>>>>>> DON'T TOUCH ANYTHING FROM HERE <<<<<<<<<<<<<<<<<<
# ---------------------------------------------------------------
# Check if another instance of this script is running
# ---------------------------------------------------------------
pidof -o %PPID -x $0 >/dev/null && echo "ERROR: Script $0 already running" && exit 1
# ---------------------------------------------------------------
# Save the current Carv node working directory
# ---------------------------------------------------------------
FOLDER_NAME=$(ls -d */ | grep 'Carv')
# ---------------------------------------------------------------
# Function to start the Carv node
# ---------------------------------------------------------------
function StartNode()
{
if [ -f "CarvScreenLog" ]; then
rm CarvScreenLog
fi
screen -mdS "Carv" -L -Logfile "CarvScreenLog"
screen -S "Carv" -X stuff 'bash'`echo -ne '\015'`
sleep 1
screen -S "Carv" -X stuff './verifier -conf ../configs/config.yaml'`echo -ne '\015'`
}
# ---------------------------------------------------------------
# Function to check for errors in log and restart node
# ---------------------------------------------------------------
function CheckAndRestartNode()
{
echo "$(date): The Carv node will be restarted now." >> CarvNodeChecker.log
# Check for error messages in the last 3 lines of CarvScreenLog
if [ -f "CarvScreenLog" ]; then
error_lines=$(tail -n 3 CarvScreenLog | grep -i "error")
if [ ! -z "$error_lines" ]; then
echo "$(date): Error found in log:" >> CarvNodeChecker.log
echo "$error_lines" >> CarvNodeChecker.log
fi
fi
ID=$(screen -ls | awk '/[0-9]+\.(.*Carv.*)/ {print $1}' | cut -d'.' -f1)
screen -XS $ID quit
StartNode
echo "$(date): The Carv node has been restarted in a new screen session." >> CarvNodeChecker.log
}
# ---------------------------------------------------------------
# Restart the node if the screen session is gone for some reason
# ---------------------------------------------------------------
if ! screen -ls | grep -q "Carv"; then
cd $FOLDER_NAME/bin
StartNode
cd
fi
# ---------------------------------------------------------------
# Check last modification time of the log file
# ---------------------------------------------------------------
LOG_FILE="$FOLDER_NAME/bin/CarvScreenLog"
if [ -f "$LOG_FILE" ]; then
last_mod_time=$(stat -c %Y "$LOG_FILE")
current_time=$(date +%s)
time_diff=$((current_time - last_mod_time))
if [ $time_diff -gt 50 ]; then
echo "$(date): The log file hasn't been updated in more than 50 seconds." >> CarvNodeChecker.log
cd $FOLDER_NAME/bin
CheckAndRestartNode
cd
fi
else
echo "$(date): Log file not found. Please check the Carv node installation." >> CarvNodeChecker.log
fi
# ---------------------------------------------------------------
# Delete the CarvScreenLog file if it's bigger than 100MB
# ---------------------------------------------------------------
if [ -f "$LOG_FILE" ]; then
# Get the file size in KB
file_size_kb=$(du -k "$LOG_FILE" | cut -f1)
# Check if the file size is greater than 100000 KB
if [ "$file_size_kb" -gt 100000 ]; then
rm "$LOG_FILE"
echo "$(date): The CarvScreenLog file has reached a size of 100MB and has therefore been deleted." >> CarvNodeChecker.log
fi
fi
Make the script executable:
chmod +x CarvNodeChecker.sh
Create the cronjob:
crontab -e
Paste the following line at the bottom of the file and remember to change USER to your user (or the whole path depending, where you saved the script):
*/1 * * * * /home/USER/CarvNodeChecker.sh
Safe the file and you’re done 🙂