Created
March 11, 2018 09:45
-
-
Save erickhun/965966b071a8150b6761c956cd7f529c to your computer and use it in GitHub Desktop.
Kill geth when it is stuck
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This bash script check if your geth is stuck in the same block number every X seconds | |
FIRST_TIME=true | |
NB_SECONDS_CHECK=300 #5 minutes | |
while true | |
do | |
JSON_RPC=$(curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83}' http://0.0.0.0:8080) | |
NEW_BLOCK_NB=$(echo $JSON_RPC | python -c 'import json,sys;obj=json.load(sys.stdin);print int(obj["result"], 0)') | |
if [ "$FIRST_TIME" = true ] ; then | |
# First call - Just memorize the block number to compare it in the next call | |
echo "Getting block number [$NEW_BLOCK_NB]" | |
FIRST_TIME=false | |
LAST_BLOCK_NUMBER=$NEW_BLOCK_NB | |
else | |
echo "Checking if geth is stuck.... [last_block: $LAST_BLOCK_NUMBER] - [new_block: $NEW_BLOCK_NB]" | |
if [ "$NEW_BLOCK_NB" -eq "$LAST_BLOCK_NUMBER" ] ; then | |
# let's kill geth. You need to have something (like supervisord) to handle geth restart | |
echo "geth stuck with the same block... Kill it'..." | |
kill -INT $(pgrep geth) | |
else | |
# All is fine - Let's update the last block to compare it again | |
LAST_BLOCK_NUMBER=$NEW_BLOCK_NB | |
fi | |
fi | |
sleep $NB_SECONDS_CHECK | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment