Skip to content

Instantly share code, notes, and snippets.

@jamesstout
Last active March 6, 2020 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesstout/6832436dd8d06adeb4aba93705ad5c39 to your computer and use it in GitHub Desktop.
Save jamesstout/6832436dd8d06adeb4aba93705ad5c39 to your computer and use it in GitHub Desktop.
Script to stop the sabnzbd queue while SpotWeb retrieve runs to avoid "Too many connections to server xxxxx.xxx" error
#!/usr/bin/env bash
# shellcheck shell=bash
# If sabnzbd is downloading at the time the spotweb retrieve script runs
# You can get the "Too many connections to server xxxxx.xxx" error.
# The script attempts to pause the sabnzbd queue,
# then wait for all connections to disconnect before running the spotweb retrieve script.
# Once it completes, the queue is started again (or reverted to initial state).
echo "Start time: $(date)"
start=$(date +%s)
if ! hash jq &>/dev/null; then
echo "You need to install jq for this to work - jq is like sed for JSON data"
echo "run: apt-get install jq"
exit 255
fi
if ! hash wget &>/dev/null; then
echo "You need to install wget for this to work - wget retrieves files from the web"
echo "run: apt-get install wget"
exit 255
fi
apikey="xxxxxxxxxxxxxxxxxxxxxxxx"
endBit="&apikey=${apikey}&output=json"
url="http://192.168.1.13:7777"
# check to see if sab is downloading
isDownloading=$(wget -qO- "${url}/api?mode=queue${endBit}" | jq -r '.queue.status')
initialStatus=${isDownloading}
echo "sabnzbd queue initialStatus is $initialStatus"
if [[ ${isDownloading} =~ "Downloading" ]]; then
echo "sabnzbd queue is $isDownloading"
# need to pause the queue
echo "pausing sabnzbd queue"
wget -qO- "${url}/api?mode=pause${endBit}" &> /dev/null
else
echo "sabnzbd queue is $isDownloading" # idle or paused
fi
# give sab some time to start disconnecting
echo "sleeping for 10s"
sleep 10
echo "check for active connections"
serveractiveconn=$(wget -qO- "${url}/api?mode=fullstatus&skip_dashboard=1${endBit}" | jq -r '.status.servers[].serveractiveconn')
echo "serveractiveconn = $serveractiveconn"
serveractiveconn=$(wget -qO- "${url}/api?mode=fullstatus&skip_dashboard=1${endBit}" | jq -r '.status.servers[].serveractiveconn')
while [ "$serveractiveconn" -gt 0 ]; do
echo "serveractiveconn = $serveractiveconn"
sleep 1
serveractiveconn=$(wget -qO- "${url}/api?mode=fullstatus&skip_dashboard=1${endBit}" | jq -r '.status.servers[].serveractiveconn')
done
echo "serveractiveconn now = $serveractiveconn, we can start to retrieve spots"
# quick double check that queue is still paused/idle
isDownloading=$(wget -qO- "${url}/api?mode=queue${endBit}" | jq -r '.queue.status')
if [[ ${isDownloading} =~ "Paused" || ${isDownloading} =~ "Idle" ]]; then
echo "sabnzbd queue is $isDownloading"
echo "Can now retrieve spots"
su -l www-data -s /usr/bin/php /var/www/spotweb/retrieve.php
echo "sleeping for 10s" # probably not needed, but just in case threre are any lingering connections
sleep 10
if [[ ! ${initialStatus} =~ "Paused" ]]; then
echo "restarting sabnzbd queue"
# restart queue
wget -qO- "${url}/api?mode=resume${endBit}" &> /dev/null
else
echo "Not restarting sabnzbd queue as it was initially ${initialStatus}"
fi
else
echo "sabnzbd queue not paused"
exit 255
fi
isDownloading=$(wget -qO- "${url}/api?mode=queue${endBit}" | jq -r '.queue.status')
if [[ ! ${isDownloading} =~ "Paused" ]]; then
echo "sabnzbd queue unpaused/idle"
else
echo "sabnzbd queue is $isDownloading"
fi
end=$(date +%s)
echo "End time: $date"
echo "Duration: $((($(date +%s)-$start)/60)) minutes"
echo "---------------------"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment