Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
btcd watchdog
#!/bin/bash
POST_INIT_SYNC_DELAY=60
POLL_DELAY=60
STALL_THRESHOLD=5
if [ -z `pidof btcd` ]; then
echo "Starting btcd"
nohup btcd &
sleep $POST_INIT_SYNC_DELAY
fi
stalls=0
while true; do
start=`btcctl --notls getinfo | jq -r .blocks`
sleep $POLL_DELAY
end=`btcctl --notls getinfo | jq -r .blocks`
echo "Processed $((end - start)) blocks in the last $POLL_DELAY seconds"
if [[ "$start" == "$end" ]]; then
if (( stalls > STALL_THRESHOLD )); then
echo "Too many stalls detected. Restarting btcd..."
kill `pidof btcd`
sleep 10
nohup btcd &
stalls=0
else
syncnode=`btcctl --notls getpeerinfo | jq -r '.[] | select(.syncnode == true) | .addr' | cut -f1 -d:`
if [ -z "$syncnode" ]; then
echo "Stall detected, but no syncnode found. Restarting btcd..."
kill `pidof btcd`
sleep 10
nohup btcd &
stalls=0
else
echo "Stall detected! Evicting potentially bad node $syncnode"
btcctl --notls node disconnect $syncnode
stalls=$(( stalls + 1 ))
fi
fi
fi
done

Sjors commented Dec 14, 2017

For OSX you'll need a replacement for pidof, e.g. brew install pidof.

I also had to remove the --notls bit, otherwise I'd get net/http: HTTP/1.x transport connection broken: malformed HTTP response "\x15\x03\x01\x00\x02\x02\x16"

Sjors commented Dec 15, 2017

This also won't work if you have multiple instances of btcd running, e.g. one for testnet and one for mainnet, because pidof btcd will just pick the first one.

guggero commented Jan 14, 2018

thanks, very useful! I've been trying to sync my btcd for three days now. Hopefully with the watchdog it will now work without interruptions.

bajohns commented Jan 21, 2018

This is working very well for me; thanks for posting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment