summaryrefslogtreecommitdiff
blob: 64c3d98884498940b97582734ade70d62551a7fd (plain)
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
#!/sbin/runscript
# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/net-nntp/sabnzbd/files/sabnzbd.initd,v 1.4 2013/03/08 02:46:45 jsbronder Exp $

# pid file name is hard-coded in sabnzbd, this must match
RUNDIR=/var/run/sabnzbd
PIDFILE=${RUNDIR}/sabnzbd-${SABNZBD_PORT}.pid

depend() {
    need net
}

start() {
    ebegin "Starting SABnzbd"

    checkpath -q -d -o ${SABNZBD_USER}:${SABNZBD_GROUP} -m 0770 "${RUNDIR}"

    start-stop-daemon \
        --quiet \
        --start \
        --user ${SABNZBD_USER} \
        --group ${SABNZBD_GROUP} \
        --name sabnzbd \
        --pidfile ${PIDFILE} \
        --exec /usr/bin/sabnzbd \
        -- \
        --server ${SABNZBD_SERVER}:${SABNZBD_PORT} \
        --config-file ${SABNZBD_CONFIGFILE} \
        --logging ${SABNZBD_LOGGING} \
        --daemon \
        --pid "${RUNDIR}"

    eend $?
}

stop() {
    local api_key=$(awk '/^api_key =/{print $3}' ${SABNZBD_CONFIGFILE})
    local hostname=${SABNZBD_SERVER}
    local rc t

    ebegin "Stopping SABnzbd"

    if [ -z "${hostname}" -o "${hostname}" == "0.0.0.0" ]; then
        hostname="localhost"
    fi

    /usr/bin/wget -q --delete-after \
        "http://${hostname}:${SABNZBD_PORT}/sabnzbd/api?mode=shutdown&apikey=${api_key}"
    rc=$?

    if [ ${rc} -eq 0 ]; then
        # Wait for sabnzbd to fully shutdown.
        for ((t=0; t < 20; t++)); do
            sleep 0.5
            [ ! -s ${PIDFILE} ] && break
        done
    fi

    if [ -s ${PIDFILE} ]; then
        # Using wget didn't work, resort to start-stop-daemon
        start-stop-daemon \
            --stop \
            --pidfile ${PIDFILE} \
            --retry SIGTERM/1/SIGKILL/5
        rc=$?
    fi

    eend ${rc}
}