Update August 7 2015 ~ start
So after having an IP or two banned because of checking to often... so, I'll provide some options on how to avoid getting banned.
First don't use the script under this update; the variable expansion Bash preforms causes multiple calls to https://blockchain.info in to short of a time frame. You can try adding sleep 120
between the assignment and calls but that would be very slow.
Second the key things are; Checking local count $(bitcoin-cli getblockcount)
and Checking a well known node $(wget -q -O - http://blockchain.info/q/getblockcount)
and Checking local sync state $(bitcoin-cli getblocktemplate)
To combine the Blockchain from your node to another in one line
$($(wget -q -O - http://blockchain.info/q/getblockcount)-$(bitcoin-cli getblockcount))
# To print this with echo and date stamp
echo " $(date) : $($(wget -q -O - http://blockchain.info/q/getblockcount)-$(bitcoin-cli getblockcount))"
# To redirect and exit with 0 : ie place in crontab
echo "$($(wget -q -O - http://blockchain.info/q/getblockcount)-$(bitcoin-cli getblockcount))" > /var/log/btc_blocks_left_to_sync.log 2>&1
The last one above is likely the best for scripting as the file it saves to can be read for just the number inside or the last time it was modifide.
Update August 7 2015 ~ end
Indeed the correct function call is shown there, @BinaryMage, thank you.
possible duplicate of When downloading the blockchain for the first time, how do I know when it's done? – BinaryMage Apr 4 '13 at 0:50
The person to show the correct function was @LoHoris, however, the updates to Bitcoin node software now uses bitcoin-cli getblocktemplate
and not bitcoind
for these lookups.
Thanks to both of them it is possible to offer you all a combination of both answers wrapped into a checker script; ready for your customization and updated for the year 2015
#!/bin/bash
timed_wait=600
vars_to_load(){
## Set vars to function such that they are set upon every call
localCheck=$(bitcoin-cli getblockcount)
crossCheck=$(wget -q -O - http://blockchain.info/q/getblockcount)
diffBlocks=$(($crossCheck-$localCheck))
localSize=$(ls -hal ~/.bitcoin/blocks | awk '/total/{print $2}')
}
check_sync(){
while true
do
current_time=$(date)
echo $current_time
bitcoin-cli getblocktemplate
if [ $? = 0 ]
then
echo "All synced up"
else
echo "Uh oh, running available checks"
## call function that runs it all on a timer
networked_sync
echo "Node has $localCheck of $crossCheck blocks available."
echo "Size of local blockchain is $localSize"
fi
echo "Sleeping for $timed_wait seconds or $(($timed_wait/60)) minuets."
sleep $timed_wait
done
}
networked_sync(){
vars_to_load
## Function to print difference in node sync
if [ $diffBlocks -eq 0 ]
then
echo "Sync is good with $diffBlocks blocks off"
else
echo "Sync has $diffBlocks blocks left to download"
fi
}
check_sync
This script first checks the exit status of bitcoin-cli getblocktemplate
because anything other than 0
is usually an error it'll run the other parts only if bitcoin-cli getblocktemplate
shows an error. Of course it is possible to check for specific error codes instead and do things for specific error codes... but this works fine as a template for me when setting up BTC nodes in a headless environment.
The other checks then compare local block count verses https://blockchain.info and prints out how many blocks your system is off. These only run when local check of sync status error out thus if you're on a slower like @Steven Roose mentioned then this script is ideal.
By default the wait time between checks is 600
seconds (10 minuets) to keep network checks and local checks to a minimum, however, for logging and better resource management it is easy enough to comment out the while
, do
, and done
and sleep $timed_wait
lines. And add one line to crontab to accomplish running on a schedule. Last modify the very end of the script to write output to a file.
The function modified;
check_sync(){
# while true
# do
current_time=$(date)
echo $current_time
bitcoin-cli getblocktemplate
if [ $? = 0 ]
then
echo "All synced up"
else
echo "Uh oh, running available checks"
## call function that runs it all on a timer
networked_sync
echo "Node has $localCheck of $crossCheck blocks available."
echo "Size of local blockchain is $localSize"
fi
echo "Sleeping for $timed_wait seconds or $(($timed_wait/60)) minuets."
# sleep $timed_wait
# done
}
And to run every 10 minuets
sudo nano /etc/crontab
*/10 * * * * root /pathTo/script.sh 2>&1
Then to enable logging modify the call that starts every thing within the script;
check_sync | tee -a /tmp/BTCsync_log.txt
Hopefully this is acceptable enough of an answer to mark this question as [Solved] such that future web searchers can have an easier time of this.