forked from WhenLamboMoon/docker-zen-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefresh.sh
More file actions
67 lines (56 loc) · 2.95 KB
/
refresh.sh
File metadata and controls
67 lines (56 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
#
# To install from web, do:
# curl -s https://raw.githubusercontent.com/aleqx/docker-zen-node/master/refresh.sh|INSTALL=1 bash
#
poll=30 # seconds, poll secure node logs every T seconds for the tracker reply
maxagochall=70 # hours, if last challenge was more than this many hours ago then quit (a challenge may be imminent)
maxagopoll=95 # seconds, if last tracker poll was more than this many seconds then wait until next tracker poll
url="https://raw.githubusercontent.com/aleqx/docker-zen-node/master/refresh.sh"
log=/root/zen-refresh.log
sh=/root/zen-refresh.sh
logkeep=1000
zenduser=$(ps -o user= -p `pgrep zend`)
zenduserhome=$(eval echo ~$zenduser)
[[ $CMDZEND ]] || CMDZEND="sudo -H -u $zenduser zend"
[[ $CMDLOGS ]] || CMDLOGS="cat $zenduserhome/.pm2/logs/secnodetracker-out*.log"
$CMDLOGS &>/dev/null || { echo "Error reading logs: $CMDLOGS. Exiting ..."; exit; }
$CMDZEND --help | grep -q rpcuser || { echo "Error executing: $CMDZEND. Exiting ..."; exit; }
[[ $1 = install || $1 = update || $INSTALL = 1 ]] && {
echo "Installing ..."
# use the same time for all servers every day
minhour=$(date -d 'Apr 4 15:35:00 CEST 2018' +'%M %H')
curl -s -o $sh "$url" && \
echo "$minhour * * 1 root bash $sh >> $log" > /etc/cron.d/zen-refresh
exit
}
# truncate log
[[ -f $log && `wc -l < $log` -ge $logkeep ]] && echo $(tail -n $logkeep $log) > $log
#export TZ=`date +'%Z %z'` # for converting times to current timezone
# EDIT: it seems that `date -d STR` does convert to the current timezone as long as STR includes
# the timezone at the end, or if it's formatted as {date}T{time}Z, which apparently is assumed GMT
lastchall=`$CMDLOGS 2>/dev/null|grep 'Challenge result'|tail -n 1`
[[ ! $lastchall && ! $FORCE ]] && echo "No challenge happened. Skipping ..." && exit # don't restart if a challenge hasn't happened (don't know if imminent)
lastchall=${lastchall/ -- *}
lastchall=`date -d"$lastchall" +%s`
agochall=$((`date +%s`-lastchall))
echo -n "`date +'%F %T'` Last challenge was $((agochall/3600))h $(((agochall%3600)/60))m ago. "
[[ $agochall -gt $((maxagochall*3600)) && ! $FORCE ]] && echo "Skipping ..." && exit # don't restart if last challenge too long ago (likely imminent)
echo
# wait for a successful stats to be received from the tracker
for ((i=600;i>0;i-=poll)); do
laststats=`$CMDLOGS 2>/dev/null|grep 'Stats received by '|tail -n 1`
[[ ! $laststats ]] && sleep $poll && continue
laststats=${laststats/ -- *}
#laststats=`date -d"$laststats" +%s`
agostats=$((`date +%s`-`date -d"$laststats" +%s`))
if [[ $agostats -gt 5 && $agostats -lt $maxagopoll ]]; then
echo "`date +'%F %T'` Last stats was at $laststats ($agostats seconds ago). Restarting zen node ..."
while pgrep zend &>/dev/null; do killall zend; sleep 3; done
$CMDZEND -daemon
exit
else
echo "`date +'%F %T'` Last stats was at $laststats ($agostats seconds ago). Waiting ..."
fi
sleep $poll
done