Switch to unified view

a b/web/init/scweb-service
1
#!/bin/sh
2
#
3
# Upmpdcli Songcast Receiver control web interface init script.
4
#         Copyright (C) 2015 J.F. Dockes
5
6
# Derived from:
7
# 
8
#         VirtualBox web service API daemon init script.
9
#
10
#         Copyright (C) 2006-2012 Oracle Corporation
11
#
12
#         This file is part of VirtualBox Open Source Edition (OSE), as
13
#         available from http://www.virtualbox.org. This file is free software;
14
#     you can redistribute it and/or modify it under the terms of the GNU
15
#     General Public License (GPL) as published by the Free Software
16
#     Foundation, in version 2 as it comes in the "COPYING" file of the
17
#     VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18
#     hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19
#
20
#
21
# chkconfig: 35 35 65
22
# description: Songcast Receiver control web interface
23
#
24
### BEGIN INIT INFO
25
# Provides:       scweb-service
26
# Default-Start:  2 3 4 5
27
# Default-Stop:   0 1 6
28
# Description:    Songcast Receiver control web interface
29
### END INIT INFO
30
31
PATH=$PATH:/bin:/sbin:/usr/sbin
32
33
[ -f /etc/debian_release -a -f /lib/lsb/init-functions ] || NOLSB=yes
34
35
if [ -n "$INSTALL_DIR" ]; then
36
    binary="$INSTALL_DIR/scweb-standalone.py"
37
    scctl="$INSTALL_DIR/scctl"
38
else
39
    binary="/usr/share/upmpdcli/web/scweb-standalone.py"
40
    scctl="/usr/bin/scctl"
41
fi
42
43
# silently exit if the package was uninstalled but not purged,
44
# applies to Debian packages only (but shouldn't hurt elsewhere)
45
[ ! -f /etc/debian_release -o -x $binary ] || exit 0
46
47
[ -r /etc/default/scweb ] && . /etc/default/scweb
48
49
system=unknown
50
if [ -f /etc/redhat-release ]; then
51
    system=redhat
52
    PIDFILE="/var/lock/subsys/scweb-service"
53
elif [ -f /etc/SuSE-release ]; then
54
    system=suse
55
    PIDFILE="/var/lock/subsys/scweb-service"
56
elif [ -f /etc/debian_version ]; then
57
    system=debian
58
    PIDFILE="/var/run/scweb-service"
59
elif [ -f /etc/gentoo-release ]; then
60
    system=gentoo
61
    PIDFILE="/var/run/scweb-service"
62
elif [ -f /etc/arch-release ]; then
63
     system=arch
64
     PIDFILE="/var/run/scweb-service"
65
elif [ -f /etc/slackware-version ]; then
66
    system=slackware
67
    PIDFILE="/var/run/scweb-service"
68
elif [ -f /etc/lfs-release ]; then
69
    system=lfs
70
    PIDFILE="/var/run/scweb-service.pid"
71
else
72
    system=other
73
    if [ -d /var/run -a -w /var/run ]; then
74
        PIDFILE="/var/run/scweb-service"
75
    fi
76
fi
77
78
if [ -z "$NOLSB" ]; then
79
    . /lib/lsb/init-functions
80
    fail_msg() {
81
        echo ""
82
        log_failure_msg "$1"
83
    }
84
    succ_msg() {
85
        log_success_msg " done."
86
    }
87
    begin_msg() {
88
        log_daemon_msg "$@"
89
    }
90
fi
91
92
if [ "$system" = "redhat" ]; then
93
    . /etc/init.d/functions
94
    if [ -n "$NOLSB" ]; then
95
        start_daemon() {
96
            usr="$1"
97
            shift
98
            daemon --user $usr $@
99
        }
100
        fail_msg() {
101
            echo_failure
102
            echo
103
        }
104
        succ_msg() {
105
            echo_success
106
            echo
107
        }
108
        begin_msg() {
109
            echo -n "$1"
110
        }
111
    fi
112
fi
113
114
if [ "$system" = "suse" ]; then
115
    . /etc/rc.status
116
    start_daemon() {
117
        usr="$1"
118
        shift
119
        su - $usr -c "$*"
120
    }
121
    if [ -n "$NOLSB" ]; then
122
        fail_msg() {
123
            rc_failed 1
124
            rc_status -v
125
        }
126
        succ_msg() {
127
            rc_reset
128
            rc_status -v
129
        }
130
        begin_msg() {
131
            echo -n "$1"
132
        }
133
    fi
134
fi
135
136
if [ "$system" = "debian" ]; then
137
    start_daemon() {
138
        usr="$1"
139
        shift
140
        bin="$1"
141
        shift
142
        start-stop-daemon --background --chuid $usr --start --exec $bin -- $@
143
    }
144
    killproc() {
145
        start-stop-daemon --stop --exec $@
146
    }
147
    if [ -n "$NOLSB" ]; then
148
        fail_msg() {
149
            echo " ...fail!"
150
        }
151
        succ_msg() {
152
            echo " ...done."
153
        }
154
        begin_msg() {
155
            echo -n "$1"
156
       }
157
    fi
158
fi
159
160
if [ "$system" = "gentoo" ]; then
161
    if [ -f /sbin/functions.sh ]; then
162
        . /sbin/functions.sh
163
    elif [ -f /etc/init.d/functions.sh ]; then
164
        . /etc/init.d/functions.sh
165
    fi
166
    start_daemon() {
167
        usr="$1"
168
        shift
169
        bin="$1"
170
        shift
171
        start-stop-daemon --background --chuid $usr --start --exec $bin -- $@
172
    }
173
    killproc() {
174
        start-stop-daemon --stop --exec $@
175
    }
176
    if [ -n "$NOLSB" ]; then
177
        fail_msg() {
178
            echo " ...fail!"
179
        }
180
        succ_msg() {
181
            echo " ...done."
182
        }
183
        begin_msg() {
184
            echo -n "$1"
185
        }
186
        if [ "`which $0`" = "/sbin/rc" ]; then
187
            shift
188
        fi
189
    fi
190
fi
191
192
if [ "$system" = "arch" ]; then
193
    USECOLOR=yes
194
    . /etc/rc.d/functions
195
    start_daemon() {
196
        usr="$1"
197
        shift
198
        su - $usr -c "$*"
199
        test $? -eq 0 && add_daemon rc.`basename $2`
200
    }
201
    killproc() {
202
        killall $@
203
        rm_daemon `basename $@`
204
    }
205
    if [ -n "$NOLSB" ]; then
206
        fail_msg() {
207
            stat_fail
208
        }
209
        succ_msg() {
210
            stat_done
211
        }
212
        begin_msg() {
213
            stat_busy "$1"
214
        }
215
    fi
216
fi
217
218
if [ "$system" = "slackware" ]; then
219
    killproc() {
220
        killall $1
221
        rm -f $PIDFILE
222
    }
223
    if [ -n "$NOLSB" ]; then
224
        fail_msg() {
225
            echo " ...fail!"
226
        }
227
        succ_msg() {
228
            echo " ...done."
229
        }
230
        begin_msg() {
231
            echo -n "$1"
232
        }
233
    fi
234
    start_daemon() {
235
        usr="$1"
236
        shift
237
        su - $usr -c "$*"
238
    }
239
fi
240
241
if [ "$system" = "lfs" ]; then
242
    . /etc/rc.d/init.d/functions
243
    if [ -n "$NOLSB" ]; then
244
        fail_msg() {
245
            echo_failure
246
        }
247
        succ_msg() {
248
            echo_ok
249
        }
250
        begin_msg() {
251
            echo $1
252
        }
253
    fi
254
    start_daemon() {
255
        usr="$1"
256
        shift
257
        su - $usr -c "$*"
258
    }
259
    status() {
260
        statusproc $1
261
    }
262
fi
263
264
if [ "$system" = "other" ]; then
265
    if [ -n "$NOLSB" ]; then
266
        fail_msg() {
267
            echo " ...fail!"
268
        }
269
        succ_msg() {
270
            echo " ...done."
271
        }
272
        begin_msg() {
273
            echo -n "$1"
274
        }
275
    fi
276
fi
277
278
check_single_user() {
279
    if [ -n "$2" ]; then
280
        fail_msg "VBOXWEB_USER must not contain multiple users!"
281
        exit 1
282
    fi
283
}
284
285
start() {
286
    if ! test -f $PIDFILE; then
287
        [ -z "$SCWEB_USER" ] && exit 0
288
        begin_msg "Starting scweb Songcast control web interface";
289
        check_single_user $SCWEB_USER
290
        echo SCWEB_USER $SCWEB_USER single ok
291
        PARAMS=""
292
        [ -n "$SCWEB_ADDRESS" ]        && PARAMS="$PARAMS -a $SCWEB_ADDRESS"
293
        [ -n "$SCWEB_PORT" ]           && PARAMS="$PARAMS -p $SCWEB_PORT"
294
295
        echo start_daemon $SCWEB_USER $binary $PARAMS
296
        start_daemon $SCWEB_USER $binary $PARAMS > /dev/null 2>&1
297
        # ugly: wait until the final process has forked
298
        sleep .1
299
        PID=`ps ax | egrep 'python .*/scweb-standalone' | grep -v grep | awk '{print $1}'`
300
        if [ -n "$PID" ]; then
301
            echo "$PID" > $PIDFILE
302
            RETVAL=0
303
            succ_msg
304
        else
305
            RETVAL=1
306
            fail_msg
307
        fi
308
    fi
309
    return $RETVAL
310
}
311
312
stop() {
313
    if test -f $PIDFILE; then
314
        begin_msg "Stopping scweb web interface";
315
        killproc $binary
316
        RETVAL=$?
317
        if ! pidof $binary > /dev/null 2>&1; then
318
            rm -f $PIDFILE
319
            succ_msg
320
        else
321
            fail_msg
322
        fi
323
    fi
324
    return $RETVAL
325
}
326
327
restart() {
328
    stop && start
329
}
330
331
status() {
332
    echo -n "Checking for scweb web interface"
333
    if [ -f $PIDFILE ]; then
334
        echo " ...running"
335
    else
336
        echo " ...not running"
337
    fi
338
}
339
340
case "$1" in
341
start)
342
    start
343
    ;;
344
stop)
345
    stop
346
    ;;
347
restart)
348
    restart
349
    ;;
350
force-reload)
351
    restart
352
    ;;
353
status)
354
    status
355
    ;;
356
setup)
357
    ;;
358
cleanup)
359
    ;;
360
*)
361
    echo "Usage: $0 {start|stop|restart|status}"
362
    exit 1
363
esac
364
365
exit $RETVAL