-
Notifications
You must be signed in to change notification settings - Fork 1
/
switchmap-daemon
executable file
·70 lines (54 loc) · 1.68 KB
/
switchmap-daemon
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
68
69
70
#!/bin/bash
#
# Josh Malone's (jmalone@nrao.edu) daemon process to run SwitchMap
# and accept requests for manual refresh from the web interface.
#
PREFIX=/opt/services/switchmap-dev
LOG=${PREFIX}/web/error-log
# How often to update the web page - in minutes
OFTEN=60
# Sleep period, in seconds - don't change
SLEEPVAL=10
# The progress message to include on the web page
PROGMSG='<span style="background: #dd1111;"> Update in progress </span><br>'
# Figure out the last time the web page was updated
last=$(stat -c %Z ${PREFIX}/web/index.html)
# Figure out what time it is now
now=$(date +%s)
echo $$ > /var/run/switchmap.pid
trap 'rm -f /var/run/switchmap.pid; exit' 0
trap 'rm -f /var/run/switchmap.pid; exit' 3
trap 'rm -f /var/run/switchmap.pid; exit' 15
# Remove stale killfile
rm -f /tmp/kill-switchmap
##################### Main loop
while :
do
elapsed=$(( (now - last) / 60 ))
[ -f /tmp/update-switchmap ] && echo "$PROGMSG" > ${PREFIX}/web/status.html
# Run the small jobs every 60 mins, regardless
if [ $elapsed -gt 60 -o -f /tmp/update-switchmap ]; then
# Re-initialize the logfile
echo "Updating SwitchMap at $(date)" >$LOG
${PREFIX}/GetArp.pl 2>>$LOG >>$LOG
${PREFIX}/ScanSwitch.pl 2>>$LOG >>$LOG
now=$((now + 5))
last=$now
# echo " done"
fi
# See if we should update the web page, too
if [ $elapsed -gt ${OFTEN:-60} -o -f /tmp/update-switchmap ]; then
echo "$PROGMSG" > ${PREFIX}/web/status.html
${PREFIX}/SwitchMap.pl 2>>$LOG >>$LOG
now=$(date +%s)
last=$now
rm -f /tmp/update-switchmap
echo -n '' > ${PREFIX}/web/status.html
fi
if [ -f /tmp/kill-switchmap ]; then
rm -f /tmp/kill-switchmap
exit
fi
sleep ${SLEEPVAL:=10}
now=$(( now + SLEEPVAL ))
done