summaryrefslogtreecommitdiff
path: root/network/qbittorrent/rc.qbittorrent-nox.new
blob: 9b78744ba4d14da0601af757b95edbdf618d1558 (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
72
73
74
75
76
77
78
79
80
81
82
#!/bin/sh

# /etc/rc.d/rc.qbittorrent-nox
# Runs qbittorrent webui under user who started the script.
# Usage: /etc/rc.d/rc.qbittorrent-nox start <PORT>|stop|restart <PORT>|status
# Port defaults to 8080 if not provided.
#
# To run this script from rc.local you must run it as a non-root user.
#
# Example:
# /bin/su - david -c /etc/rc.d/rc.qbittorrent-nox start 9000
# Program output is sent to /tmp/qbittorrent-nox-$USER

# First some checks to see what's what.

if [ "$USER" = "root" ] && [ "$1" = "start" ]; then
    echo "Do not start the daemon as root." >/dev/stderr
    exit 1
fi


if [ -n "$2" ]; then
    UIPORT="$2"
else
    UIPORT="8080"
fi

LOG="/tmp/qbittorrent-nox-$USER"
APP="/usr/bin/qbittorrent-nox"

do_start()
{
    if [ -n "$(/bin/netstat -nta | awk '{print $4}' \
         | cut -d: -f2 | grep $UIPORT | grep 0.0.0.0)" ]; then

        echo "Port $UIPORT is already in use." >/dev/stderr
        exit 1
    fi

    $APP --webui-port=$UIPORT 1>$LOG 2>&1 &
}

do_stop()
{
    PID="$(pgrep -u $USER qbittorrent-nox)"

    if [ -n "$PID" ]; then
        echo "Killing PID $PID"
        kill $PID
    else
        echo "No process found." >/dev/stderr
    fi
}

do_status()
{
    echo "Local Address	Foreign Address	State	PID/Program name"
    /bin/netstat -pntl 2>&1 | grep qbittorrent-n \
    | awk '{print $4 "	" $5 "	" $6 "	" $7}'
}

case "$1" in
    'start')
        do_start
    ;;

    'stop')
        do_stop
    ;;
    'restart')
        do_stop
        sleep 1
        do_start
    ;;
    'status')
        do_status
    ;;
    *)
        echo "Usage: $0 {start <PORT>|stop|restart <PORT>|status}"
        exit 1
    ;;
esac