Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setting up a rai_node for Ubuntu
#!/bin/bash
#######################################################################
## ##
## This script installs and configures a Nano (RaiBlocks) ##
## Network Node v17.0.0 in an Ubuntu installation. ##
## It adds also some useful commands to control the node. ##
## Use it at your own risk. ##
## For other systems just take out|adapt the apt commands ##
## ##
#########################################
VERSION=nano-17.0-Linux.tar.bz2
EXEC=rai-17.0.0-Linux/bin/rai_node
echo "This script will install and configure a Nano RaiBlocks node v11 on Ubuntu"
if [[ $EUID -ne 0 ]]; then
echo "Please run this script as user root."
exit 1
fi
echo "hardening ssh login... Don't fail login attempts too much!"
apt update && apt dist-upgrade && apt clean
apt install -y fail2ban curl
cd /root
wget https://github.com/nanocurrency/raiblocks/releases/download/V17.0VERSION
tar xf $VERSION
rm $VERSION
echo "Adding swapfile for better syncing..."
fallocate -l 2G /swapfile
mkswap /swapfile
chmod 600 /swapfile
swapon /swapfile
#creating some scripts to manage the node
echo "Creating scripts...."
cd /usr/local/bin
echo '#!/bin/bash' | tee -a clear_unchecked stop_node start_node blockcount run_check peers version bootstrap
printf "curl -g -d '{ \"action\": \"block_count\" }' '[::ffff:127.0.0.1]:7076' " >> blockcount
printf "curl -g -d '{ \"action\": \"unchecked_clear\" }' '[::ffff:127.0.0.1]:7076' " >> clear_unchecked
printf "curl -g -d '{ \"action\": \"frontiers\" }' '[::ffff:127.0.0.1]:7076' " >> frontiers
printf "curl -g -d '{ \"action\": \"stop\" }' '[::ffff:127.0.0.1]:7076' " >> stop_node
printf "curl -g -d '{ \"action\": \"peers\" }' '[::ffff:127.0.0.1]:7076' " >> peers
printf "curl -g -d '{ \"action\": \"version\" }' '[::ffff:127.0.0.1]:7076' " >> version
printf "curl -g -d '{ \"action\": \"stop\" }' '[::ffff:127.0.0.1]:7076' " >> stop_node
printf "curl -g -d '{ \"action\": \"bootstrap_any\" }' '[::ffff:127.0.0.1]:7076' " >> bootstrap
printf "%s\n" "RUNNING=\$(curl --connect-timeout 3 --max-time 5 -g -d '{\"action\":\"version\" }' '[::ffff:127.0.0.1]:7076')" ' if [ $? -ne 0 ]; then' 'killall rai_node && sleep 4' " /usr/local/bin/start_node" 'echo "No RPC response - restarted $(date +"%a %e %B %H:%M")" >> /root/node.log' "fi" >> run_check
printf "%s\n" "/root/$EXEC --daemon &" 'echo "$i startet at $(date +"%a %e %B %H:%M")" >> /root/node.log' >> start_node
chmod +x *
cd /root
start_node
sleep 10
killall rai_node
#setup curl
echo "Configuring config.json for curl comands...."
sed -i 's/"rpc_enable": "false"/"rpc_enable": "true"/g' RaiBlocks/config.json
sed -i 's/"enable_control": "false"/"enable_control": "true"/g' RaiBlocks/config.json
sed -i 's/"address": "::1"/"address": "::ffff:127.0.0.1"/g' RaiBlocks/config.json
echo "Adding a crontab to run a node running check every 20 min..."
(crontab -l 2>/dev/null; echo "*/20 * * * * /usr/local/bin/run_check") | crontab -
start_node
sleep 5
echo "Node is now running and syncing. This can take some hours."
sleep 2
echo "Type 'blockcount' to control sync state, 'stop_node' to stop, 'start_node' to start the node."
sleep 2
echo "'clear_unchecked' will clear unchecked blocks; if your node is falling behind a lot try 'bootstrap' or restart it."
sleep 2
echo "A cron script will check automatically if the node is running and restart it if necessary."
echo "Enjoy, bye"
echo "PS: when the curl commands like 'blockcount' do not respond: this means the node hangs, so issue in this case 'killall rai_node' and again 'start_node'"
@schott12521

This comment has been minimized.

Copy link

commented Mar 30, 2018

For anyone looking for a fix script that restarts the node whenever it stops / hangs instead of waiting 20 minutes you can try this for run_check

while true; do
    RUNNING=$(/usr/local/bin/blockcount |grep count|wc -l)
    if [ $RUNNING = 1 ]; then
        #Process running
        echo "Running at $(date +"%a %e %B  %H:%M:%S")" >> /root/node.log
    else
        #Process not running / hanging
        killall rai_node
        echo "Found RPC dead - restarted $(date +"%a %e %B %H:%M:%S")"  >> /root/node.log
        /usr/local/bin/start_node
        sleep 40
    fi
    sleep 5
done

where sleep 5 is how long to wait before checking if the server is alive. be warned, this is making an RPC call, ideally in the future this will not have to be used.

@stefonarch

This comment has been minimized.

Copy link
Owner Author

commented Mar 30, 2018

My script now, still for cron:

#!/bin/sh
RUNNING=$(curl --connect-timeout 3 --max-time 5 -s -g -d '{"action":"version" }' '[::ffff:127.0.0.1]:7076')
        if [ $? -ne 0 ]; then
                #No response to RPC calls
        killall rai_node
        sleep 3
        /usr/local/bin/start_node
                echo "No RPC response, Node restartet at $(date +"%a %e %B  %H:%M")" >> /root/node.log 
        else
                #Process not running
                echo "Node running  at $(date +"%a %e %B %H:%M")"  >> /root/node.log
        fi


EDIT: the script uses this watchdog now as run_check done by cron.


@GusRuss89

This comment has been minimized.

Copy link

commented Apr 1, 2018

Does the restart script on a cron negate the need to run the node as a service?

@stefonarch

This comment has been minimized.

Copy link
Owner Author

commented Apr 2, 2018

Good question, I think in this case you should change the command:
systemctl restart service <nanonode>

@n1ghtshade

This comment has been minimized.

Copy link

commented Apr 23, 2018

Hey, this script comes in pretty handy!
Maybe when you get time, would it be possible to get a second version, which would additionally create ramdisk, mount it, start node using ramdisk, and also
Sync to disk daily, and sync back to ramdisk & restart node automatically after reboot?

I tried modifying the script myself to achieve all this, but I'm not so good with bash, so I just broke it :(

@stefonarch

This comment has been minimized.

Copy link
Owner Author

commented Apr 27, 2018

It's possible, but maybe better another script, I'll look into it.

@n1ghtshade

This comment has been minimized.

Copy link

commented May 2, 2018

Line 24 needs to be wget https://github.com/nanocurrency/raiblocks/releases/download/V12.1/$VERSION

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.