-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcommon
153 lines (134 loc) · 2.76 KB
/
common
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
#!/bin/sh
. /lib/lsb/init-functions
FILE=`basename "$0"`
DEVICE_ID="${FILE##*.}"
APP_DIR="/opt/piBeacon"
NODE_APP="app$DEVICE_ID.js"
CONFIG_DIR="$APP_DIR"
PID_DIR="$APP_DIR/pid"
PID_FILE="$PID_DIR/app$DEVICE_ID.pid"
LOG_DIR="$APP_DIR/log"
LOG_FILE="$LOG_DIR/forever$DEVICE_ID.log"
APP_LOG_FILE="$LOG_DIR/app$DEVICE_ID.log"
APP_ERR_FILE="$LOG_DIR/err$DEVICE_ID.log"
NODE_EXEC="/usr/local/bin/forever"
. /etc/default/piBeacon
USAGE="Usage: $0 {start|stop|restart|status}"
pid_file_exists() {
[ -f "$PID_FILE" ]
}
get_pid() {
echo "$(cat "$PID_FILE")"
}
is_running() {
PID=$(get_pid)
! [ -z "$(ps aux | awk '{print $2}' | grep "^$PID$")" ]
}
start_it() {
mkdir -p "$PID_DIR"
mkdir -p "$LOG_DIR"
if [ -z "$DEVICES" ]
then
log_daemon_msg "Cannot start piBeacon. Please fill in DEVICES in /etc/default/piBeacon"
log_end_msg 1
elif [ -z "$NAME" ]
then
log_daemon_msg "Cannot start piBeacon. Please fill in NAME in /etc/default/piBeacon"
log_end_msg 1
elif [ -z "$INTERVAL" ]
then
log_daemon_msg "Cannot start piBeacon. Please fill in INTERVAL in /etc/default/piBeacon"
log_end_msg 1
fi
if [ $DEVICES -gt "0" ] && [ "$DEVICE_ID" -lt "$DEVICES" ]
then
log_daemon_msg "Starting piBeacon $DEVICE_ID"
BLENO_HCI_DEVICE_ID="$DEVICE_ID" FOREVER_ROOT="$APP_DIR/.forever" $NODE_EXEC -s -a -l $LOG_FILE -o $APP_LOG_FILE -e $APP_ERR_FILE --pidFile $PID_FILE start "$APP_DIR/$NODE_APP"
while [ ! -f $PID_FILE ]
do
sleep 1
done
log_end_msg $?
else
log_daemon_msg "Cannot start piBeacon $DEVICE_ID. Increase the number of devices, currently $DEVICES"
log_end_msg 1
fi
}
stop_process() {
FOREVER_ROOT="$APP_DIR/.forever" $NODE_EXEC stop "$APP_DIR/$NODE_APP"
}
remove_pid_file() {
echo "Removing pid file"
rm -f "$PID_FILE"
}
start_app() {
if pid_file_exists
then
if is_running
then
PID=$(get_pid)
echo "Node app already running with pid $PID"
exit 1
else
echo "Node app stopped, but pid file exists"
echo "Forcing start anyways"
remove_pid_file
start_it
fi
else
start_it
fi
}
stop_app() {
if pid_file_exists
then
if is_running
then
echo "Stopping node app ..."
stop_process
remove_pid_file
echo "Node app stopped"
else
echo "Node app already stopped, but pid file exists"
echo "Forcing stop anyways ..."
remove_pid_file
echo "Node app stopped"
fi
else
echo "Node app already stopped, pid file does not exist"
exit 1
fi
}
status_app() {
if pid_file_exists
then
if is_running
then
PID=$(get_pid)
echo "Node app running with pid $PID"
else
echo "Node app stopped, but pid file exists"
fi
else
echo "Node app stopped"
fi
}
case "$1" in
start)
start_app
;;
stop)
stop_app
;;
restart)
stop_app
start_app
;;
status)
status_app
;;
*)
echo $USAGE
exit 1
;;
esac