Bash scripts for managing safe nodes on Linux

I am planning to run and manage nodes on Linux using short scripts. I want to keep things flexible, modular, transparent and to have maximum control. At least initially. I will consider GUIs, TUIs, managers and launchers later.

Perhaps this topic can serve as a place to collect and share scripts, or links to scripts elsewhere on this forum, so that we don’t need to reinvent the wheel and to help inspiring better scripts.

Edit: Now a Wiki: We can add quick links to scripts arranged by purpose:

BASH:

PYTHON:

12 Likes
  • Launch nodes at designated port numbers
    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.) An alternative is commented-out in the script. Replace with server’s local IP address.
for (( c=53300; c<=53349; c++ ))
do
   #safenode --ip=<IP> --port $c --max_log_files=10 --max_archived_log_files=0 2>&1 > /dev/null & disown
   screen -LdmS "safenode$c" safenode --ip=<IP> --port $c --max_log_files=10 --max_archived_log_files=0
   echo "starting node on next port following $c with 360 second delay..."
   sleep 360
done
3 Likes
  • Find and record ports and PIDs of existing node peer ids
    Rocky Linux 9.4, bash
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)
  echo "$iPORT <-> $iPID"
  echo "$iPID" > ~/.local/share/safe/node/$i/PID
  echo "$iPORT" > ~/.local/share/safe/node/$i/PORT
done
2 Likes
  • 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
  • List node wallet balances
    Rocky Linux 9.4, bash
for i in ~/.local/share/safe/node/*; do safe wallet balance --peer-id=$i | grep "balance"; done
2 Likes
  • Get the number of node peer ids on system (running or dead)
    Rocky Linux 9.4, bash
ls ~/.local/share/safe/node | wc -l
2 Likes
  • Get number of node peer-ids that are alive
    Rocky Linux 9.4, bash
lsof ~/.local/share/safe/node/*/logs/safenode.log | grep "safenode " | wc -l
2 Likes
  • Anonymize own IP address from your logs before sharing
    Rocky Linux 9.4, bash
    Warning: This irreversibly changes all logs. It may be better to do this on a copy. The example below assumes your public IP is 51.222.110.119
cd ~/.local
find . -type f -exec sed -i 's/51.222.110.119/99.999.999.999/g' {} +
1 Like

why not just use safe node manager ?

here is the resources script from the community git hub for resource logging.

1 Like

It’s a personal preference, maybe because I don’t have enough time to try out new tools. I feel I need quick-and-dirty, simple but flexible, easy-to-understand tools. Additional layers of abstraction introduce unknowns, reduce control and increase dependency on others and the level of testing done, and generally, I try to avoid adding dependencies, using frameworks and other technology layers until I really can’t avoid it.
However, I may have to switch to using node manager in the future, for more complex operations such as upgrading nodes with minimal downtime, or some other approach for migrating nodes between systems for example (if that even makes sense vs. deletion and starting a new node).

3 Likes

You might want to include setting the RPC port because you can then easily run scripts doing RPC calls to find info directly from the node, like balances & status

There is a curl like project for doing rpc calls. grpcurl on github

4 Likes

Sound like you are meaning business great to have you on board.

If you up for sharing and collaborating there is the community git hub page if your interested @Southside could add you on to it ?

1 Like

I was not aware of this dashboard. GitHub - safenetforum-community/NTracking: NTacking Dashboard for Autonomi Network. Looks nice. Looks great!

3 Likes

Thanks It’s joint effort between myself and @Josh.

If you try it out it needs the nodes started with node manager to use the status command for stats and the nodes storing logs in the node manager location for greping errors.

3 Likes

Found it: GitHub - fullstorydev/grpcurl: Like cURL, but for gRPC: Command-line tool for interacting with gRPC servers Thanks!

5 Likes

If you make the OP a Wiki you can add a list of links, one to each reply with a script.

A Wiki stays editable forever and others can add links too.

That would be good, I just don’t know how to make it a wiki. Do I need to request it, or is there a setting somewhere that I can change myself?

If you have enough forum clout you can click the “…” under the topic to expand the options and then click the spanner. If you don’t, ping a mod to do it for you.

2 Likes

Asking works wonders. :sunglasses: Its now a wiki

2 Likes

It can’t start nodes with --upnp flag. The next release can, I guess, but right now I’d like to have a script that starts nodes with configurable delay with --upnp flag. Anyone?