以太坊geth api訪問,區塊同步監測
curl查詢geth區塊高度
supervisor管理以太坊geth進程
geth進程健康檢查
# curl訪問geth api
#使用curl訪問geth api查詢區塊高度
curl -s -X POST -H "Content-Type":application/json \
--data '{"jsonrpc":"2.0", "method":"eth_blockNumber","params":[],"id":67}' \
localhost:8545 |awk -F'"' '{print $(NF-1)}'
#如上,查詢結果為十六進制
#在shell終端查看十進制區塊高度
echo $((`curl -s -X POST -H "Content-Type":application/json --data \
'{"jsonrpc":"2.0", "method":"eth_blockNumber","params":[],"id":67}' \
localhost:8545 |awk -F'"' '{print $(NF-1)}'`))
# 使用supervisor管理以太坊geth進程
#安裝啟動supervisor(ubuntu)
apt-get install -y supervisor
systemctl start supervisor
systemctl enable supervisor
#配置geth
mkdir -p /var/log/geth
vim /etc/supervisor/conf.d/geth.conf
#配置文件如下
[program:geth]
command=/opt/geth/geth --rpc --rpcapi web3,eth,net,db,personal --rpcaddr 0.0.0.0 --rpcport 8545
directory=/opt/geth
user=root
autostart=true
autorestart=true
startretries=9999
exitcodes=0
stopsignal=KILL
stopwaitsecs=10
redirect_stderr=true
logfile_backups=10
stdout_logfile_maxbytes=8MB
stdout_logfile=/var/log/geth/geth.log
#使用supervisor啟動geth
supervisorctl reload
supervisorctl restart geth
有時geth進程運行正常,區塊同步故障,需要檢查區塊高度是否增長
使用curl訪問api查詢區塊高度,間隔一段時間在查,對比沒增長則重啟進程
shell分享如下:
#!/bin/bash
# check.geth.sh
# By Elven , 2018-11-16
#區塊高度監控
#定時任務
#check blockchain
#*/4 * * * * bash /opt/shell/check.geth.sh
#var
eth_api=localhost:8545
Stime=180
[ $1 -gt $Stime ] && { Stime=$1 ; }
H1=$((`curl -s -X POST -H "Content-Type":application/json --data '{"jsonrpc":"2.0", "method":"eth_blockNumber","params":[],"id":67}' $eth_api |awk -F'"' '{print $(NF-1)}'`))
sleep $Stime
H2=$((`curl -s -X POST -H "Content-Type":application/json --data '{"jsonrpc":"2.0", "method":"eth_blockNumber","params":[],"id":67}' $eth_api |awk -F'"' '{print $(NF-1)}'`))
if [[ $H1 -eq $H2 ]];then
echo "geth restart at $(date +%F" "%T) block $H1" >>/tmp/geth.restart.log
supervisorctl restart geth &>/dev/null
fi