/
bridge
executable file
·189 lines (175 loc) · 3.87 KB
/
bridge
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
pid=0
filestart="/home/pi/node-flower-bridge/start.js"
filetest="/home/pi/node-flower-bridge/test.js"
usage () {
echo "Flower Bridge: See Flowers fall on the bridge"
echo "Every 15 minutes (default), all of your Flower Power will be update"
echo
echo "USAGE: ./bridge cmd [delay]"
echo
echo "ARGUMENTS:"
echo " cmd:"
echo " display: Lannch and show updating into your console"
echo " background: Launch updatind to background. To know what happening: \`$ ./updatedb\`"
echo " restart: Stop the current program and re-launch. If no program is running, 'restart' is similar as 'background'"
echo " status: Show if the program is running or not"
echo " stop: Stop all processings and kill the program"
echo
echo " delay (optional):"
echo " Specify the loop time in minute"
echo
echo "EXAMPLES:"
echo " ./bridge display : (loop every 15 minutes)"
echo " ./bridge display 60 : (loop every 1 hour)"
echo " ./bridge background 1440 : (loop every day without render in console)"
echo " ./bridge stop : (Stop the program)"
echo " ./bridge restart : (Restart the program)"
echo
echo "AUTHOR:"
echo " Written by Bruno Sautron."
}
welcome () {
echo
echo "-- Flower Bridge --"
echo
}
pidexist () {
kill -0 $1 2> /dev/null
}
# @return
# 0 -> running
# 1 -> stoping
# 2 -> waiting
isrunning () {
pid=$(head -n 1 "/home/pi/node-flower-bridge/.flowerIsRunning" 2> /dev/null)
if [ -z "$pid" ]
then
echo "[---] No program is running"
return 1
else
pidexist $pid
if [ "$?" == 0 ]
then
echo "[running] A program is running - $pid"
return 0
else
crontab -u pi -l | grep -n "node-flower-bridge\/bridge background$" > /dev/null
if [ $? == 0 ]
then
echo "[waiting] A program is on waiting, it will restart it self "
return 2
fi
fi
echo "[--+] No program is running"
return 1
fi
}
statusit () {
welcome
isrunning
exit 0
}
displayit () {
welcome
isrunning
status="$?"
if [ "$status" == 1 ]
then
echo node $1 $2
node $1 $2
else
stopit
node $1 $2
fi
}
backgroundit () {
welcome
isrunning
status="$?"
if [ "$status" == 1 ]
then
node $1 $2 2> /home/pi/node-flower-bridge/trace.log 1> /dev/null &
echo $! > "/home/pi/node-flower-bridge/.flowerIsRunning"
echo "Starting bridge in background"
echo "node $1 $2 2> trace.log 1> /dev/null &"
echo "$> ./bridge status"
installit
elif [ "$status" == 2 ]
then
node $1 $2 2> /home/pi/node-flower-bridge/trace.log 1> /dev/null &
echo $! > "/home/pi/node-flower-bridge/.flowerIsRunning"
echo "The bridge wakup"
fi
}
stopit () {
isrunning $> /dev/null
status="$?"
if [ "$status" == 0 ]
then
killit
crontab -u pi -l | sed "/node-flower-bridge\/bridge background$/d" | crontab
echo "Bye :)"
elif [ "$status" == 2 ]
then
crontab -u pi -l | sed "/node-flower-bridge\/bridge background$/d" | crontab
fi
}
restartit () {
stopit
backgroundit $1 $2
}
killit () {
kill $pid
rm -f "/home/pi/node-flower-bridge/.flowerIsRunning"
echo "kill" $pid
pid=0
}
installit () {
crontab -l | grep -n "node-flower-bridge\/bridge background$" > /dev/null
if [ "$?" == 1 ]
then
crontab -u pi -l > /home/pi/node-flower-bridge/mycron
echo "*/15 * * * * /home/pi/node-flower-bridge/bridge background" >> /home/pi/node-flower-bridge/mycron
crontab -u pi /home/pi/node-flower-bridge/mycron
rm /home/pi/node-flower-bridge/mycron
fi
}
if [ -z "$1" ]
then
usage
else
if [ "$1" == "install" ]
then
installit
elif [ "$1" == "display" ]
then
displayit $filestart $2
elif [ "$1" == "background" ]
then
backgroundit $filestart $2
elif [ "$1" == "stop" ]
then
stopit
elif [ $1 == "restart" ]
then
restartit $filestart $2
elif [ $1 == "running" ]
then
isrunning
elif [ $1 == "crontab" ]
then
echo pi:
crontab -u pi -l
echo root:
sudo crontab -l
elif [ $1 == "status" ]
then
statusit
elif [ $1 == "test" ]
then
displayit $filetest $2
else
usage
fi
fi