86 lines
1.9 KiB
Bash
Executable File
86 lines
1.9 KiB
Bash
Executable File
#! /usr/bin/env bash
|
|
|
|
# qbittorrent-nox.sh start|stop DIR PORT
|
|
# DIR will contain the qbittorrent-nox profile, as well as "pid" and "qBottorrent.log"
|
|
# PORT configures port for qbittorrent-nox web API
|
|
|
|
# Wait for qBittorrent WEB API to come online at address $1
|
|
# waitforqBittorrentStart "http://localhost:1312"
|
|
function waitforqBittorrentStart() {
|
|
#set +x
|
|
TIMESTAMP=$(date +%s)
|
|
END=$((TIMESTAMP+10))
|
|
ERR=0
|
|
while true; do
|
|
NEWTIMESTAMP=$(date +%s)
|
|
if [ $NEWTIMESTAMP -gt $END ]; then
|
|
ERR=1
|
|
break
|
|
fi
|
|
if curl --silent "$1" 2>&1 > /dev/null; then
|
|
break
|
|
else
|
|
sleep 0.1
|
|
fi
|
|
done
|
|
return $ERR
|
|
}
|
|
|
|
# Wait for qBittorrent to be done cleanly exiting
|
|
# Necessary because otherwise it will leave temporary files behind!
|
|
# waitforQbittorrentStop 1234
|
|
function waitforqBittorrentStop() {
|
|
TIMESTAMP=$(date +%s)
|
|
END=$((TIMESTAMP+15))
|
|
ERR=0
|
|
while true; do
|
|
NEWTIMESTAMP=$(date +%s)
|
|
if [ $NEWTIMESTAMP -gt $END ]; then
|
|
ERR=1
|
|
break
|
|
fi
|
|
if ! ps x | grep -P "^\s+$PID\s+" 2>&1 > /dev/null; then
|
|
# process died successfully
|
|
break
|
|
else
|
|
sleep 0.1
|
|
fi
|
|
done
|
|
return $ERR
|
|
}
|
|
|
|
start() {
|
|
echo "y" | qbittorrent-nox --profile="$1" --webui-port="$2" 2>&1 > "$1"/qBittorrent.log &
|
|
echo $! > "$1"/pid
|
|
if waitforqBittorrentStart "http://localhost:$2"; then
|
|
return 0
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
stop() {
|
|
PID="$(cat "$1"/pid)"
|
|
kill $PID
|
|
if ! waitforqBittorrentStop $PID; then
|
|
echo "qBittorrent does not quit. Using SIGKILL"
|
|
kill -9 $PID
|
|
fi
|
|
}
|
|
|
|
case "$1" in
|
|
"start")
|
|
QBTNOX_DIR="$2"
|
|
QBTNOX_PORT="$3"
|
|
start "$QBTNOX_DIR" "$QBTNOX_PORT"
|
|
STATUS=$?
|
|
;;
|
|
"stop")
|
|
QBTNOX_DIR="$2"
|
|
stop "$QBTNOX_DIR"
|
|
STATUS=$?
|
|
;;
|
|
esac
|
|
|
|
exit $STATUS
|