-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.sh
executable file
·116 lines (90 loc) · 1.8 KB
/
monitor.sh
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
#
# Monitor Script for Fire-Midi Services
#
# Keeps usb-relay
# websocketd
#
#
# running
#
#
#files
LOGFILE="/tmp/monitor.txt"
RESTART="/tmp/restart"
RESTART_MIDI="/tmp/restart_midi"
RESTART_WEBSOCK="/tmp/restart_websock"
PID_DIR="/tmp"
BIN_DIR="/home/pi"
WEBSOCKETD="websocketd"
USBRELAY="usb-relay"
MIDI="midi2relay"
# kill $1
kill()
{
local ret=1
local PID="$PID_DIR/$1.pid"
local pid
if [ -e "$PID" ]; then
pid2=`cat $PID`;
if [ -d /proc/$pid2 ]; then
echo "OK: $1 is running kill it" >> $LOGFILE
kill $pid2
sleep 3
else
echo "FAIL: $1 is dead, cleanup pid file" >> $LOGFILE
rm "$PID"
fi
fi
}
#check $1
isRunning()
{
local ret=1
local PID="$PID_DIR/$1.pid"
local pid
if [ -e "$PID" ]; then
pid2=`cat $PID`;
if [ -d /proc/$pid2 ]; then
ret=1
else
echo "FAIL: $1 is dead, cleanup pid file" >> $LOGFILE
rm "$PID"
fi
fi
if [ ! -e "$PID" ]; then
# start it up
"$BIN_DIR/run-$1" &
pid2=$(ps ax |grep "$1" |grep -v "grep" |awk '{ print $1 }')
echo $pid2 > $PID
ret=0
echo "OK: $1 has started" >> $LOGFILE
fi
return $ret
}
#
# Init here
logger "[FireMidi] monitor starting up"
echo "Startig Up" >> $LOGFILE
sleep 1
#
# Main Loop, this runs forever when device is running up
#
while [ 1 ]
do
# check if restart files are written
if [ -f $RESTART ]; then
logger "[FireMidi] restart"
echo "Restart All" >> $LOGFILE
rm $RESTART
killall $WEBSOCKETD
killall $USBRELAY
killall $MIDI
sleep 2
fi
isRunning $WEBSOCKETD
isRunning $USBRELAY
isRunning $MIDI
# check ever 15 seconds
sleep 15
done