January 11, 2025

Autorestart of Shardeum node

A Python-based script that checks the validator status every 60 seconds and runs it if necessary.

1. Updating package list:sudo apt update

2. Installing Python 3: sudo apt install python3

3. Installing a Package Manager for Python: sudo apt install python3-pip

4. Create a "screen" session (install "screen" if necessary): screen -S validator_checker

5. Go to the directory with the node: cd ~/shardeum

6. Create a file that will contain the script text (if necessary, install the "nano" editor): nano validator_checker.py

7. In the opened field (file "validator_checker.py") insert the script text - see at the end of the article.

8. Save the script text in a file - click Ctrl+O, Enter, Ctrl+X.

9. Run the script: python3 validator_checker.py

10. Exit the "screen" session: Ctrl+A+D

import subprocess import time

# Function for executing a command in a Docker container def run_command(command): try: result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT) return result.decode('utf-8') except subprocess.CalledProcessError as e: return f"Error executing command: {e.output.decode('utf-8')}"

# Main function to check status and run validator def check_validator_status(): print("Checking validator status...")

# Executing a command to obtain validator status status_command = "sudo docker exec -it shardeum-validator operator-cli status" status_output = run_command(status_command)

if "state: stopped" in status_output: print("Validator stopped, trying to start...") # Executing the command to start the validator start_command = "sudo docker exec -it shardeum-validator operator-cli start" start_output = run_command(start_command) print("The validator has been launched.") else: print("The validator works fine.")

def main(): while True: check_validator_status() time.sleep(60) # Pause 60 seconds before next check

if __name__ == "__main__": main()

If you want to thank me with a donation... my EVM wallet (any tokens of any compatible network) - 0x938339c8b0382AB9F157529e2214cA943f266738