Skip to content

Instantly share code, notes, and snippets.

@MSiccDev
Last active September 8, 2019 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MSiccDev/81d511148623d3de3cb83fbe744941d4 to your computer and use it in GitHub Desktop.
Save MSiccDev/81d511148623d3de3cb83fbe744941d4 to your computer and use it in GitHub Desktop.
Script to check the current sync state of bitcoin and similar nodes
#based on samples I found here:
#https://bitcoin.stackexchange.com/questions/37815/how-to-know-if-bitcoind-synced
#https://bitcoin.stackexchange.com/questions/10859/how-to-check-bitcoind-block-chain-download-progress-level/12734#12734
#you may not need sudo if you are running bitcoind as local user instead of using bitcoin.service
#as local user, the cli is found in /usr/local/bin, change commented lines below
#!/bin/bash
coin="$1";
if [ -z $coin ]; then
/bin/echo "No coin selected, using Bitcoin"
coin="bitcoin";
fi
bc_height=$(wget -O - http://blockchain.info/q/getblockcount 2>/dev/null);
/bin/echo "Last known block (blockchain.info): $bc_height";
#count=$(/usr/local/bin/${coin}-cli getblockcount);
count=$(sudo /usr/bin/${coin}-cli getblockcount);
/bin/echo "Last local block: $count";
/bin/echo $count $bc_height | /usr/bin/awk '{printf "Synch state: %s of %s (%.2f%%)\n", $1, $2, ($1/$2)*100}';
#hash=$(/usr/local/bin/${coin}-cli getblockhash $count);
hash=$(sudo /usr/bin/${coin}-cli getblockhash $count);
#timestamp=$(/usr/local/bin/${coin}-cli getblock "$hash" | grep '"time"' | awk '{print $2}' | sed -e 's/,$//g');
timestamp=$(sudo /usr/bin/${coin}-cli getblock "$hash" | grep '"time"' | awk '{print $2}' | sed -e 's/,$//g');
/bin/echo "Last local block timestamp is: $timestamp";
now=$(date +%s);
timediff=$[$now - $timestamp];
/bin/echo -n "Sync time offset: ";
/bin/echo $timediff | /usr/bin/awk '{printf "%d days, %d:%d:%d\n", $1/(60*60*24), $1/(60*60)%24, $1%(60*60)/60, $1%60}';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment