Guest User

Script To Automatically Restart Monerod

a guest
Jan 2nd, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.52 KB | Cryptocurrency | 0 0
  1. #=============== Script monero.sh =======
  2. #!/bin/bash
  3. # TODO: Change this.
  4. location_bitmonero="/path/to/.bitmonero/"
  5.  
  6. current_dir=$(pwd)
  7. pushd  $location_bitmonero || cd $location_bitmonero
  8.  
  9. PATH=/opt/bin/:$PATH
  10. export PATH
  11.  
  12. DNS_PUBLIC="tcp://127.0.0.1"
  13. #DNS_PUBLIC="tcp://9.9.9.9"
  14. export DNS_PUBLIC
  15.  
  16. MONERO_RANDOMX_FULL_MEM=1
  17. export MONERO_RANDOMX_FULL_MEM
  18.  
  19. #
  20. # $1 = file, $2 = max age in hrs - both mandatory.
  21. function is_file_recent () {
  22.     local retval=0 # defaults to 0, false
  23.  
  24.     file_tmstmp=$(stat -c %Y $1)
  25.     now_tmstmp=$(date +%s)
  26.     file_age=$(($now_tmstmp - $file_tmstmp))
  27.     #max_age=$(( $2 * 60 )) # for debug (in mins)
  28.     max_age=$(( $2 * 60 * 60))
  29.  
  30.     if [[ $file_age -gt $max_age ]]; then
  31.         # "Max age exceeded re-download"
  32.         retval=0
  33.     else
  34.         # "File is recent no download"
  35.         retval=1
  36.     fi
  37.     echo ${retval}
  38. }
  39.  
  40. # Download the latest XMR nodes
  41. # https://monero.fail/haproxy.cfg
  42. # https://monero.fail/haproxy.cfg?chain=monero&network=mainnet
  43. function download_haproxy () {
  44.     echo -ne "\nDownloading latest Active Peers.\n"
  45.     wget -q -O haproxy.cfg "https://monero.fail/haproxy.cfg?chain=monero&network=mainnet"
  46. }
  47.  
  48. re_download=$(is_file_recent "haproxy.cfg" 6)
  49. if [[ $re_download -eq 0 ]]; then
  50.     download_haproxy
  51. else
  52.     echo Active Peers File is new enough!
  53. fi
  54.  
  55. # ***************************************
  56. peer_nodes=()
  57. success_count=0
  58. MAX_PEER_NODES=15
  59. for anode in $(cat haproxy.cfg |   grep -E '\s+server backend-.+{1,5}\s+' | awk '{print $3}' | grep -vE 'b32\.i2p|\.onion')
  60. do
  61.     server=$(echo "$anode" | sed  's/\(.*\):\(.*\)/\1/')
  62.     echo -ne "\n===================================\n"
  63.     echo -ne "Checking if Monero Node : $server is alive"
  64.     ping -c 1 -W 5 $server 1>/dev/null 2>&1
  65.     if [[ $? -eq 0 ]] then
  66.         echo -ne " -> [Yes]\n===================================\n"
  67.         # append to array.
  68.         peer_nodes+=($anode)
  69.         ((success_count+=1))
  70.     else
  71.         echo -ne " -> [No]\n===================================\n"
  72.     fi
  73.  
  74.     if [[ $success_count -eq $MAX_PEER_NODES ]] then
  75.        break
  76.     fi
  77. done
  78. add_peer_node_str=""
  79. for pnode in ${peer_nodes[@]}
  80. do
  81.     add_peer_node_str+=" --seed-node $pnode "
  82. done
  83. # ***************************************
  84.  
  85. function download_banlist () {
  86.     # Download the latest ban list.  
  87.     # https://github.com/Boog900/monero-ban-list/blob/main/ban_list.txt
  88.     echo -ne "\nDownloading BanList File.\n\n"
  89.     aria2c --allow-overwrite --remote-time -o ban_list.txt https://raw.githubusercontent.com/Boog900/monero-ban-list/refs/heads/main/ban_list.txt
  90.     # bug fix
  91.     touch ban_list.txt
  92. }
  93.  
  94. echo -ne "\n"
  95. do_download=$(is_file_recent "ban_list.txt" 6)
  96. if [[ $do_download -eq 0 ]]; then
  97.     download_banlist
  98. else
  99.     echo "========================================"
  100.     echo        BanList File is new enough!
  101.     echo "========================================"
  102.     echo ""
  103. fi
  104.  
  105.  
  106. cmd="monerod --zmq-pub tcp://127.0.0.1:18083 "
  107.  
  108. # Peer Node(s)
  109. cmd+=$add_peer_node_str
  110.  
  111. cmd_heredoc=$( cat <<'EndCmd'
  112.  --enforce-dns-checkpointing  --enable-dns-blocklist --ban-list  /path/to/ban_list.txt
  113. --limit-rate-up 512
  114. --limit-rate-down 512
  115. --out-peers 100 --in-peers 100
  116. --max-connections-per-ip 3
  117. --p2p-bind-port 18080 --p2p-bind-ip 192.188.1.4 --p2p-external-port 18080
  118. --log-file /mnt/crypto/.bitmonero/bitmonero.log --max-log-files 2
  119. --no-igd --igd=disabled --db-sync-mode safe:sync --max-concurrency 6
  120. --pidfile /path/to/monerod.pid  --detach
  121. --rpc-bind-ip 127.0.0.1  --rpc-bind-port 18081  
  122. --public-node --confirm-external-bind
  123. --rpc-restricted-bind-port 18089 --rpc-restricted-bind-ip 192.188.1.4
  124. --rpc-payment-allow-free-loopback
  125. --rpc-ssl-allow-any-cert
  126. --rpc-ssl enabled
  127. --log-level 0  
  128. EndCmd
  129. )
  130.  
  131. cmd+=$cmd_heredoc
  132. echo $cmd
  133.  
  134. echo "Countdown ends in 10"
  135. for a in {1..9}
  136. do
  137.     echo -ne $a.
  138.     sleep 1
  139. done
  140. echo "10"
  141.  
  142. sudo -u monero -g monero --preserve-env=DNS_PUBLIC,PATH,MONERO_RANDOMX_FULL_MEM  ${cmd}
  143.  
  144. popd || cd "$current_dir"
  145.  
  146.  
  147. #=============== Script monero-restart.sh =======
  148. #!/usr/bin/bash
  149.  
  150. # Stop the monerod daemon.
  151. curl -k -v -X POST https://127.0.0.1:18081/stop_daemon -H 'Content-Type: application/json'
  152.  
  153. # Pause before restart
  154. echo "Restarting in 30"
  155. sleep 30
  156.  
  157. # restart the daemon
  158. /path/to/bin/monero.sh
  159.  
  160.  
  161.  
  162.  
  163. # ============================= Cron job =========
  164. # Execute script
  165. SHELL=/bin/bash
  166. PATH=/path/to/bin/:/sbin:/bin:/usr/sbin:/usr/bin
  167. * 7 * * * root /path/to/bin/monero-restart.sh 1>>/path/to/bitmonero.log
Add Comment
Please, Sign In to add comment