Bash scripts for managing safe nodes on Linux

  • Stop running nodes within a port range.
    Rocky Linux 9.4, bash
MINPORT=30900
MAXPORT=30950
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=$(netstat -lnup 2> /dev/null | grep "$iPID/safenode" | grep -o "[0-9]*" | head -7 | tail -n1)
  if [[ $iPORT -le $MAXPORT && $iPORT -ge $MINPORT ]]; then
    echo "stopping node peer-id $i at port $iPORT"
    echo "$iPORT" > ~/.local/share/safe/node/$i/PORT
    kill $iPID
  else
    echo "skipping pid $iPID on port $iPORT (not in range or not running)"
  fi
done
2 Likes