Synology and Deluge (Restart and Check Torrents)

General support for problems installing or using Deluge
Post Reply
darkpoet1978

Synology and Deluge (Restart and Check Torrents)

Post by darkpoet1978 »

After a few weeks off work and playing heavily with my Synology NAS, Entware, and Deluge, I came up with two scripts that have help them run together, minimizing errors and any babysittting.

First, let me introduce you to my restart script. This will check the age of the Deluge log file and if it is too old, can trigger a system reboot. Please note that you will need to edit the script that starts the Deluge Daemon (/opt/etc/init.d/S80deluged). Add some sort of option to create a logfile. I use the same options as the variable "makerun" in the script below. Of course, you may also need to change the location of the logfile.

I set this script to run in my Synology scheduler every 10 minutes. If the log file is older than 15 minutes, then the script will attempt to restart the daemon. If the daemon is not restarted in 90 seconds, the system will reboot. Yes, this may not be behavior you wish for a NAS. I have other reliability options in place but 5 minutes of downtime is worth it to keep Deluge running... especially when I tweaked the settings and overloaded my memory (15 downloads isn't good on my NAS's minimal RAM).

Code: Select all

#!/bin/bash
#make-run.sh
#checks to see if deluged is running, attempts to start it again before rebooting...
#...will also try if deluged if it is running but log file is too old
#location /opt/etc/deluge/make-run.sh
#set to run every 10 minutes
#edited from https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=87782

process="deluged"
delugelogfile="/volume1/Downloads/deluge.log"
logfile="/volume1/Downloads/deluge-makerun.log"
#makerun="/opt/etc/init.d/S80deluged start"
makerun="/opt/bin/deluged -L info -l /volume1/Downloads/deluge.log"


	if ps ax | grep -vw grep | grep -w $process > /dev/null
	then
		echo Already running...
		# 15min (x 60seconds) too old
		if [ `stat --format=%Y $delugelogfile` -le $(( `date +%s` - ( 60 * 15 ) )) ]; then 
			msg="Log file is older than 15 minutes... Force-killing and restarting Deluge daemon..."
			echo $msg.
			echo $msg: >>$logfile
			date >>$logfile
			echo - >>$logfile
			killall -s 9 deluged
			sleep 30
			echo $makerun &
		fi
	else
		msg="Attempting restart of Deluge daemon..."
		echo $msg.
		echo $msg: >>$logfile
		date >>$logfile
		echo - >>$logfile
		echo $makerun &
	fi

	sleep 90

	if ps ax | grep -vw grep | grep -w $process > /dev/null
	then
		echo Deluge daemon is running.
		exit
	else
		msg="Deluge daemon still not running so triggering system reboot."
		echo $msg.
		echo $msg: >>$logfile
		date >>$logfile
		echo - >>$logfile
		reboot
	fi

exit
My second script makes up for a shortcoming of Deluge, namely that it doesn't autocheck torrents that show up as "Error." This is especially useful to keep torrents running even after the above script executes a forced reboot and Deluge comes back. Please note, you will need to edit this script for your username and password and log location.

I set this script to run after boot with the Synology scheduler, where I also put "sleep 600" before executing the script. If you don't do that, then you should uncomment the sleep command below. I allow a generous 10 minutes to make sure my NAS has had time to come fully online. You may wish less or more.

Code: Select all

#!/bin/bash
#checkerrors.sh
#this should check if there any torrents with errors and trigger a recheck
#location /opt/etc/deluge/checkerrors.sh

#set to run after boot (you may need a sleep 10 minutes / 600 seconds delay here or in the scheduler)
#sleep 600

dfolder=/opt/bin
ddaemon=127.0.0.1:8111
username=admin
pwd=XXXXXXXXX
logfile="/volume1/Downloads/deluge-checkerrors.log"

cd $dfolder

echo Running checkerrors.sh >>$logfile
date >>$logfile

myarray=( $($dfolder/deluge-console "connect $ddaemon $username $pwd;info" | grep -i 'ID:\|State: Error' | awk '{ print $2 }') )
arraylength=${#myarray[@]}

for (( i=1; i<${arraylength}+1; i++ ));
do
	if [ "${myarray[$i-1]}" == "Error" ]; then
		echo Restarting ${myarray[$i-2]};
		echo Restarting ${myarray[$i-2]} >>$logfile;
		$dfolder/deluge-console "connect $ddaemon $username $pwd;recheck ${myarray[$i-2]}";
	fi;
done

echo Finished. >>$logfile
date >>$logfile
echo - >>$logfile
Both of these scripts should work on any low-level linux system like Tomato USB or an off-the-shelf 2-Bay NAS like mine. Of course, I don't guarantee this will work with the Synology package of Deluge. As I said, I installed Entware to mine and don't regret it. I also edited to my auth file... not sure if that is relevant but I use the second login for my above script.

And before anybody mentions it, I'm no Linux guru. I'm just comfortable with a command line and very good at using Google. I didn't even know how to use arrays in BASH before working on this little project.
cron410

Re: Synology and Deluge (Restart and Check Torrents)

Post by cron410 »

The script below is a bit simpler and should work on more systems. I have not tested on Synology as I do not have one. Any future imrovements to this script can be found on Github Here or Here

Code: Select all

#!/bin/bash
#this should be run as the user running deluged service on the local deluge machine or container

#logfile="/config/deluge-checkerrors.log"
dconsole=$(which deluge-console)
dfolder=$(dirname $dconsole)

cd $dfolder
myarray=( $($dconsole -c /config "info" | grep -B 1 'State: Error' | grep -v -e "--" -e Error | awk '{ print $2 }') )

#echo Checking for failed torrents in Deluge >>$logfile
echo "[info] Checking for failed torrents in Deluge"
#date >>$logfile
#echo "${#myarray[@]} torrents in error state">>$logfile

if [[ ${#myarray[@]} -eq $zero ]];
  then echo "[info] ${#myarray[@]} torrents in error state.";
  else echo "[warn] ${#myarray[@]} torrents in error state, rechecking torrents.";
fi

for torrents in "${myarray[@]}"
do
  $dconsole -c /config "recheck $torrents"
done

#echo Finished. >>$logfile
#echo -- >>$logfile
Post Reply