Skip to content

Instantly share code, notes, and snippets.

@jleskovar
Last active January 12, 2019 19:15
Show Gist options
  • Select an option

  • Save jleskovar/dfc545148398d81715da02f61bf39b91 to your computer and use it in GitHub Desktop.

Select an option

Save jleskovar/dfc545148398d81715da02f61bf39b91 to your computer and use it in GitHub Desktop.
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
@neogeno

neogeno commented Aug 9, 2018

Copy link
Copy Markdown

This helped a lot

@ccdle12

ccdle12 commented Oct 3, 2018

Copy link
Copy Markdown

Thank you, very helpful

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