以太坊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
原文地址:https://www.cnblogs.com/elvi/p/10205777.html
时间: 2024-11-13 11:03:43