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
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
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.