The KIP 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 KIPScreenLog was created during the screen creation)
  • It deletes the KIPScreenLog file if it reaches 100MB

Things to know:

  • There must only be one KIP folder present in the home directory (it can be called anything but KIP should be part of the name)
  • The script creates a new screen session, which is called KIP
  • It creates a log file called KIPScreenLog next to the node binary file inside the KIP node folder

The Script

First, create a new shell file:

cd && nano KIPNodeChecker.sh

Copy the script and paste it into the file:

#!/usr/bin/env bash
#
# KIP Node Checker v1.0
# 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


# ---------------------------------------------------------------
# Create variables
# ---------------------------------------------------------------
KIPNODECHECKER_LOG="/home/ciasis/KIPNodeChecker.log"


# ---------------------------------------------------------------
# Save the current KIP node working directory
# ---------------------------------------------------------------
FOLDER_NAME=$(ls -d */ | grep 'KIP')


# ---------------------------------------------------------------
# Function to start the KIP node
# ---------------------------------------------------------------
function StartNode() {
screen -mdS "KIP" -L -Logfile "KIPScreenLog"
screen -S "KIP" -X stuff 'bash'$'\015'
sleep 1
screen -S "KIP" -X stuff 'kipnode license start --all'$'\015'
}


# ---------------------------------------------------------------
# Function to check for errors in log and restart node
# ---------------------------------------------------------------
function CheckAndRestartNode() {
echo "$(date): The KIP node will be restarted now." >> "$KIPNODECHECKER_LOG"

# Check for error messages in the last 3 lines of KIPScreenLog
if [ -f "KIPScreenLog" ]; then
error_lines=$(tail -n 2 KIPScreenLog | grep -i "error")
if [ -n "$error_lines" ]; then
echo "$(date): Error found in log:" >> "$KIPNODECHECKER_LOG"
echo "$error_lines" >> "$KIPNODECHECKER_LOG"
fi
fi

ID=$(screen -ls | awk '/[0-9]+\.(.*KIP.*)/ {print $1}' | cut -d'.' -f1)
screen -XS "$ID" quit
StartNode
echo "$(date): The KIP node has been restarted in a new screen session." >> "$KIPNODECHECKER_LOG"
}


# ---------------------------------------------------------------
# Restart the node if the screen session is gone for some reason
# ---------------------------------------------------------------
if ! screen -ls | grep -q "KIP"; then
if ! cd "$FOLDER_NAME"; then
echo "$(date): Failed to change to KIP directory" >> "$KIPNODECHECKER_LOG"
exit 1
fi
echo "$(date): The KIP node has been restarted, because no KIP screen session has been found." >> "$KIPNODECHECKER_LOG"
StartNode
if ! cd; then
echo "$(date): Failed to return to home directory" >> "$KIPNODECHECKER_LOG"
exit 1
fi
fi


# ---------------------------------------------------------------
# Check last modification time of the log file
# ---------------------------------------------------------------
LOG_FILE="$FOLDER_NAME/KIPScreenLog"
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." >> "$KIPNODECHECKER_LOG"
if ! cd "$FOLDER_NAME"; then
echo "$(date): Failed to change to KIP directory" >> "$KIPNODECHECKER_LOG"
exit 1
fi
CheckAndRestartNode
if ! cd; then
echo "$(date): Failed to return to home directory" >> "$KIPNODECHECKER_LOG"
exit 1
fi
fi
else
echo "$(date): Log file not found. Please check the KIP node installation." >> "$KIPNODECHECKER_LOG"
fi


# ---------------------------------------------------------------
# Delete the KIPScreenLog file if it's bigger than 100MB
# ---------------------------------------------------------------
if [ -f "$LOG_FILE" ]; then
# Get the file size in KB
file_size_kb=$(stat -c %s "$LOG_FILE" | awk '{print $1/1024}')

# Check if the file size is greater than 100000 KB
if [ "${file_size_kb%.*}" -gt 100000 ]; then
rm "$LOG_FILE"
echo "$(date): The KIPScreenLog file has reached a size of 100MB and has therefore been deleted." >> "$KIPNODECHECKER_LOG"
fi
fi

Edit this line to match your home directory (replace “ciasis” with your user name):
KIPNODECHECKER_LOG=”/home/ciasis/KIPNodeChecker.log”

Make the script executable:

chmod +x KIPNodeChecker.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, on where you saved the script):

*/1 * * * * /home/USER/KIPNodeChecker.sh

Save the file and you’re done 🙂