Bash scripts for managing safe nodes on Linux

  • Restart node ids as needed at original port (requires original PORT number recorded in file ~/.local/share/safe/node/$i/PORT)
    Rocky Linux 9.4, bash
    Runs each node process in its own “screen.” List screens with screens -ls. (Enter screen with -r and exit pressing Ctrl-a d.)
for i in $(ls ~/.local/share/safe/node/)
do
  iPID=$(lsof ~/.local/share/safe/node/$i/logs/safenode.log | grep "safenode " | grep -o "[0-9]*" | head -1)
  iPORT=$(cat ~/.local/share/safe/node/$i/PORT)
  if [[ ! $iPID -gt 0 ]]; then  # continue if there is an active PID
    if [[ $iPORT -gt 0 ]]; then # continue if a record of original port number is available
      echo "starting node peer-id $i at port $iPORT"
      screen -LdmS "safenode$iPORT" safenode --port $iPORT --root-dir ~/.local/share/safe/node/$i --log-output-dest ~/.local/share/safe/node/$i/logs --max_log_files=10 --max_archived_log_files=0
      sleep 360
    fi
  else
    echo "Skipping start of safenode$iPORT (node id $i) due to existing process $iPID or non-existent PORT record."
  fi
done

Variant limiting restarts to a port range:

MINPORT=30900
MAXPORT=30999
for i in $(ls ~/.local/share/safe/node/)
do
  iPID=$(lsof ~/.local/share/safe/node/$i/logs/safenode.log | grep "safenode " | grep -o "[0-9]*" | head -1)
  iPORT=$(cat ~/.local/share/safe/node/$i/PORT)
  if [[ ! $iPID -gt 0 ]]; then  # continue if there is an active PID
    if [[ $iPORT -gt 0 ]]; then # continue if a record of original port number is available
      if [[ $iPORT -le $MAXPORT && $iPORT -ge $MINPORT ]]; then
        echo "starting node peer-id $i at port $iPORT"
        screen -LdmS "safenode$iPORT" safenode --port $iPORT --root-dir ~/.local/share/safe/node/$i --log-output-dest ~/.local/share/safe/node/$i/logs --max_log_files=10 --max_archived_log_files=0
        sleep 20
      fi
    fi
  else
    echo "Skipping start of safenode$iPORT (node id $i) due to existing process $iPID or non-existent PORT record."
  fi
done
2 Likes