-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathemonhub
executable file
·238 lines (218 loc) · 7.1 KB
/
emonhub
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#! /bin/sh
###
#
# This code is released under the GNU Affero General Public License.
#
# OpenEnergyMonitor project:
# http://openenergymonitor.org
#
###
### BEGIN INIT INFO
# Provides: emonhub
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/stop emonHub
### END INIT INFO
NAME=emonhub
DAEMONUSER="emonhub"
SYSCONF_PATH=/etc/default/
# If there's no configuration file, die!!
[ ! -f $SYSCONF_PATH$NAME ] && echo "Fatal: cannot find $SYSCONF_FILE$NAME config file!" && exit 1
# Pull in the config settings and echo them out for easy troubleshooting.
. $SYSCONF_PATH$NAME
[ ! -f $EMONHUB_CONFIG ] && echo "Fatal: cannot find $EMONHUB_CONFIG" && exit 1
[ ! -f $EMONHUB_PATH$NAME.py ] && echo "Fatal: cannot find $EMONHUB_PATH$NAME.py" && exit 1
[ ! -f $EMONHUB_LOG ] && sudo mkdir -p "$(dirname "$EMONHUB_LOG")" && sudo touch "$EMONHUB_LOG" && sudo chown -R $DAEMONUSER "$(dirname "$EMONHUB_LOG")"
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="OpenEnergyMonitor emonHub"
# Enter your installation path here
DAEMON=$EMONHUB_PATH$NAME.py
# Specify command line arguments here
DAEMON_ARGS="--logfile $EMONHUB_LOG --config-file $EMONHUB_CONFIG"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || echo "Not starting - cannot find $DAEMON" || exit 1
# Exit if not run as root
case `id -u` in
0) ;;
*) echo "This script must be run as root" >&2; exit 1 ;;
esac
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Override VERBOSE setting
VERBOSE=yes
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start() {
do_status
case "$?" in
0) return 1 ;;
1) rm -f $PIDFILE;;
3) ;;
4) PID=`ps -ef | grep '[p]ython '$DAEMON | awk '{ print $2 }'`
echo "$PID" > $PIDFILE;;
*) return "$?" ;;
esac
start-stop-daemon --start --quiet --background --make-pidfile --chuid=$DAEMONUSER --pidfile $PIDFILE --startas $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --background --no-close --make-pidfile --chuid=$DAEMONUSER --pidfile $PIDFILE --startas $DAEMON -- \
$DAEMON_ARGS \
|| return 2
# Add code here, if necessary, that waits for the process to be ready
# to handle requests from services started subsequently which depend
# on this one. As a last resort, sleep for some time.
}
#
# Function that stops the daemon/service
#
do_stop() {
do_status
case "$?" in
0) ;;
1) rm -f $PIDFILE;;
3) return 1 ;;
4) PID=`ps -ef | grep '[p]ython '$DAEMON | awk '{ print $2 }'`
echo "$PID" > $PIDFILE;;
*) return "$?" ;;
esac
start-stop-daemon --stop --quiet --retry=INT/30/KILL/5 --pidfile $PIDFILE >/dev/null || RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
rm -f $PIDFILE
return "$?"
}
#
# Function that restarts the daemon/service
#
do_restart() {
do_stop
case "$?" in
0|1) ;;
*) return 2;;
esac
do_start
[ "$?" = 0 ] && return 0
return 2
}
#
# Function that status checks the daemon/service
#
do_status() {
status="0"
pidofproc -p $PIDFILE $DAEMON >/dev/null || status="$?"
if [ "$status" = 3 ]; then
pid=`ps -ef | grep '[p]ython '$DAEMON | awk '{ print $2 }'`
[ "$pid" != "" ] && return 4
fi
return $status
}
#
# Function that sends a SIGHUP to the daemon/service
#
#do_reload() {
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
# start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
# return 0
#}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0) [ "$VERBOSE" != no ] && log_progress_msg "has been started ok"
[ "$VERBOSE" != no ] && log_end_msg 0 ;;
1) [ "$VERBOSE" != no ] && log_progress_msg "is already running"
[ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_progress_msg "start attempt" #failed
[ "$VERBOSE" != no ] && log_end_msg 1 ;;
*) [ "$VERBOSE" != no ] && log_progress_msg "error: $?"
[ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0) [ "$VERBOSE" != no ] && log_progress_msg "has been stopped ok"
[ "$VERBOSE" != no ] && log_end_msg 0 ;;
1) [ "$VERBOSE" != no ] && log_progress_msg "had already stopped"
[ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_progress_msg "stop attempt" #failed
[ "$VERBOSE" != no ] && log_end_msg 1 ;;
*) [ "$VERBOSE" != no ] && log_progress_msg "error: $?"
[ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
[ "$VERBOSE" != no ] && log_daemon_msg "Checking $DESC" "$NAME"
do_status
case "$?" in
0) [ "$VERBOSE" != no ] && log_progress_msg "process is running"
[ "$VERBOSE" != no ] && log_end_msg 0 ;;
1) [ "$VERBOSE" != no ] && log_progress_msg "process has" #failed
[ "$VERBOSE" != no ] && log_end_msg 1 ;;
3) [ "$VERBOSE" != no ] && log_progress_msg "process not running"
[ "$VERBOSE" != no ] && log_end_msg 0 ;;
4) [ "$VERBOSE" != no ] && log_progress_msg "running but PID file has" #failed
[ "$VERBOSE" != no ] && log_end_msg 1 ;;
*) [ "$VERBOSE" != no ] && log_progress_msg "status unknown as checks" #failed
[ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
#reload|force-reload)
#
# If do_reload() is not implemented then leave this commented out
# and leave 'force-reload' as an alias for 'restart'.
#
#log_daemon_msg "Reloading $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
[ "$VERBOSE" != no ] && log_daemon_msg "Restarting $DESC" "$NAME"
do_restart
case "$?" in
0) [ "$VERBOSE" != no ] && log_progress_msg "has been restarted ok"
[ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_progress_msg "restart attempt has" #failed
[ "$VERBOSE" != no ] && log_end_msg 1 ;;
*) [ "$VERBOSE" != no ] && log_progress_msg "error: $?"
[ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
killp)
# use only for crash testing - kills process without removing the pid file
# also used to "tidy up" a process that's missing it's pid file (eg if rmpid used)
# can be commented out or deleted, (not included in "Usage" help list).
pid=`ps -ef | grep '[p]ython '$DAEMON | awk '{ print $2 }'`
[ "$pid" != "" ] && kill -9 "${pid:-}"
;;
rmpid)
# use only for crash testing - removes the pid file without killing process
# also used to "tidy up" a crashed process's redundant pid file (eg if killp used)
# can be commented out or deleted, (not included in "Usage" help list).
[ -e $PIDFILE ] && rm $PIDFILE
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
: