forked from Parrot-Developers/node-flower-bridge
/
bridge
executable file
·148 lines (136 loc) · 2.52 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
#!/bin/bash
pid=0
filestart="./start.js"
filetest="./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
}
isrunning () {
pid=$(head -n 1 ".flowerIsRunning" 2> /dev/null)
if [ -z "$pid" ]
then
return 1
else
pidexist $pid
if [ "$?" == 0 ]
then
return 0
fi
return 1
fi
}
statusit () {
isrunning
if [ "$?" == 0 ]
then
echo "[$pid] A program is running"
else
echo "No program is running"
fi
}
displayit () {
welcome
isrunning
if [ "$?" == 1 ]
then
node $1 $2
else
echo "[$pid]: A program was already running"
stopit
node $1 $2
fi
}
backgroundit () {
welcome
isrunning
if [ "$?" == 1 ]
then
node $1 $2 2> trace.log 1> /dev/null &
echo $! > ".flowerIsRunning"
echo "To see your log:"
echo "$> ./updatedb"
else
echo "[$pid]: A program is already running"
fi
}
stopit () {
isrunning
if [ "$?" == 1 ]
then
echo "No program is running"
else
killit
echo "Bye :)"
fi
}
restartit () {
stopit
backgroundit $1 $2
}
killit () {
kill $pid
rm -f ".flowerIsRunning"
echo "kill" $pid
pid=0
}
if [ -z "$1" ]
then
usage
else
if [ "$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 == "status" ]
then
statusit
elif [ $1 == "test" ]
then
displayit $filetest $2
else
usage
fi
fi