当脚本运行重新启动服务的命令时,我想重新启动循环计数器。由于某种原因,只有当我完全终止脚本并重新启动它时,它才会重置它。
### CONFIGURATION
# path to jcli
JCLI="jcli"
JCLI_PORT=3100
LAST_BLOCK=""
START_TIME=$SECONDS
# this sets the counter file and it does indeed write it back to 0 when the script is started and stopped.
TEMPFILE=/tmp/counter.tmp
echo 0 > $TEMPFILE
# block log file
BLOCK_LOG=/home/coconut/logs/block.log
# how many seconds should we wait if no blocks show up
RESTART_GT=240
# display output headers
echo "/////////////////////////////////////////////////////////////////////////////////////"
echo "///////////////////////// JORMUNGANDR NODE MONITOR //////////////////////////////////"
echo "/////////////////////////////////////////////////////////////////////////////////////"
echo ""
echo "TODAY DATE | EP | SLOT# | EXP TIME | LOC TIME | HEIGHT | LAST HASH | COUNTER"
echo ""
echo "/////////////////////////////////////////////////////////////////////////////////////" >> ${BLOCK_LOG}
echo "///////////////////////// JORMUNGANDR NODE MONITOR //////////////////////////////////" >> ${BLOCK_LOG}
echo "/////////////////////////////////////////////////////////////////////////////////////" >> ${BLOCK_LOG}
echo "" >> ${BLOCK_LOG}
echo "TODAY DATE | EP | SLOT# | EXP TIME | LOC TIME | HEIGHT | LAST HASH | COUNTER" >> ${BLOCK_LOG}
echo "" >> ${BLOCK_LOG}
while true
do
DATE=$(date '+%Y-%m-%d')
TIME=$(date '+%H:%M:%S')
LAST_HASH=$($JCLI rest v0 node stats get --host "http://127.0.0.1:${JCLI_PORT}/api" | grep lastBlockHash | awk '{print $2}' | cut -c 1-10)
LATEST_BLOCK=$($JCLI rest v0 node stats get --host "http://127.0.0.1:${JCLI_PORT}/api" | grep lastBlockHeight | awk '{print $2}' | rev | cut -c 2- | rev | cut -c 2-)
LATEST_SLOT=$($JCLI rest v0 node stats get --host "http://127.0.0.1:${JCLI_PORT}/api" | grep lastBlockDate | awk '{print $2}' | rev | cut -c 2- | rev | cut -c 5- )
LAST_BLOCK_TIME=$($JCLI rest v0 node stats get --host "http://127.0.0.1:${JCLI_PORT}/api" | grep lastBlockTime | awk '{print $2}' | cut -c 13- | rev | cut -c 8- | rev)
EPOCH=$($JCLI rest v0 node stats get --host "http://127.0.0.1:${JCLI_PORT}/api" | grep lastBlockDate | awk '{print $2}' | cut -c -3 | cut -c 2- )
if [ "$LATEST_BLOCK" > 0 ]; then
if [ "$LATEST_BLOCK" != "$LAST_BLOCK" ]; then
COUNTER=$((COUNTER+1))
START_TIME=$(($SECONDS))
echo "${DATE} | ${EPOCH} | ${LATEST_SLOT} | ${LAST_BLOCK_TIME} | ${TIME} | 0${LATEST_BLOCK} | ${LAST_HASH} | ${COUNTER}"
echo "${DATE} | ${EPOCH} | ${LATEST_SLOT} | ${LAST_BLOCK_TIME} | ${TIME} | 0${LATEST_BLOCK} | ${LAST_HASH} | ${COUNTER}" >> ${BLOCK_LOG}
LAST_BLOCK="$LATEST_BLOCK"
else
ELAPSED_TIME=$(($SECONDS - $START_TIME))
if [ "$ELAPSED_TIME" -gt "$RESTART_GT" ]; then
echo "//////////////////////////////////////////////////////////////////////////////////"
echo "${DATE} | ${TIME} | Restarting Jormungandr. | Waited ${ELAPSED_TIME} for block."
echo "//////////////////////////////////////////////////////////////////////////////////" >> ${BLOCK_LOG}
echo "${DATE} | ${TIME} | Restarting Jormungandr. | Waited ${ELAPSED_TIME} for block." >> ${BLOCK_LOG}
sudo service jorg restart
LAST_BLOCK="$LATEST_BLOCK"
echo "Sleeping for 90 sec."
sleep 90
## this is where it should reset the counter to 0 since the script is being restarted
echo 0 >! ${TEMPFILE}
fi
fi
else
echo "No block height"
# Reset time
START_TIME=$(($SECONDS))
fi
sleep 20
done
exit 0
答案1
命令
echo 0 >! ${TEMPFILE}
将 a0
和变量的内容写入TEMPFILE
名为 的文件!
。它相当于
echo 0 ${TEMPFILE} > "!"
我认为你想做的是
echo 0 > "$TEMPFILE"
或者可能
echo 0 >| "$TEMPFILE"
这将截断名为 by 的文件$TEMPFILE
,并将0
后跟换行符写入该文件。
我不完全确定你用这个文件做什么,因为你只在其中写入零。但上面的行是您在脚本中评论的行,它的重定向确实存在问题。
其他随机评论:
您多次调用
$JCLI rest v0 node stats get --host "http://127.0.0.1:${JCLI_PORT}/api"
.如果这需要花费任何时间或使用任何资源,那么最好只执行一次并将结果保存到稍后解析多次的文件中。管道
grep lastBlockTime | awk '{print $2}' | cut -c 13- | rev | cut -c 8- | rev
看起来过于复杂。您很可能可以通过一次调用sed
或来完成此操作awk
,例如awk '/lastBlockTime/ { print substr($2, 13, 8) }'
或类似的操作。您使用了许多未加引号的变量。如果它们的值在某些时候可能包含空格或通配模式字符,请考虑对它们进行双引号以避免出现问题。
末尾的
exit 0
屏蔽了脚本末尾的任何错误,因此您无法调用该脚本,然后检测它是否运行正常。该脚本缺少
#!
指定用于运行脚本的解释器的行。