Friday, May 16, 2008

Enhanced transmission script for your DNS-323

I miss the ability to auto-stop/shutdown the bittorrent client after all downloads have completed hence I decided to port the old mldonkey script over. Here is a quick summary on what it does.
  • Improved the robustness of the startup, shutdown and restart sequence.
  • Added function to auto-stop transmission when there are no more active downloads.
  • Added function to auto-shutdown the box when there are no more active downloads.
  • Added function to configure the behavior when there are no more active downloads. Valid arguments are:
    • set_stop_when_done - stops transmission when there are no more active downloads;
    • set_off_when_done - shutdowns the box when there are no more active downloads;
    • set_donothing_when_done - let transmission continue to run when there are no active downloads (i.e. seeding);
  • Use the function softstop_status to check what is the current configuration option.

The default behavior is set_stop_when_done. Command syntax is . /ffp/start/transmission.sh [cmd]. Here is the script below.



#!/ffp/bin/sh

###################################################################
# author: Sidney Chong (bfg100k[at]gmail.com) #
# version: 0.1 #
# date: 15/05/2008 #
# #
# Version History #
# --------------- #
# v0.1 - expanded original startup script to be more robust and #
# include auto-stop or auto-shutdown the box when #
# downloads are completed. #
# #
###################################################################

if [ -z ${TRANSMISSION_HOME} ]; then
#setup the ENV variables if not found
#this can happen when running from crontab
echo "Environment variables not found, including default fun_plug profile."
. /ffp/etc/profile
fi

TMP_CRONTAB="/ffp/log/crontab.tmp"

name="transmission-daemon"
command="/ffp/bin/transmission-daemon"
user=nobody

transmission_start()
{
#check if daemon is already running. if yes, do nothing
if transmission_status grep -q "is running"; then
echo "INFO: ${name} already running! Nothing to do."
return
fi

if [ ! -d $TRANSMISSION_HOME ]; then
su $user -c "mkdir $TRANSMISSION_HOME"
fi
echo "Starting ${name}..."
su $user -c "$command -f -v 2 1>$TRANSMISSION_HOME/$name.log 2>&1 &"
sleep 1
if [ -S $TRANSMISSION_HOME/daemon/socket ]; then
chmod 0777 $TRANSMISSION_HOME/daemon/socket
else
echo "Could not find Transmission socket"
return
fi

#this step configures what to do if there are no more active downloads
#default (stop_when_done) is to stop transmission.
#change to off_when_done if you want to shutdown the box
#change to donothing_when_done if you want to let it run (e.g. for seeding)
_transmission_setup_softstop stop_when_done

echo "Transmission startup sequence completed. Check ${TRANSMISSION_HOME}/${name}.log for more details."
}

transmission_stop()
{
if transmission_status grep -q "is running"; then
echo -n "Stopping ${name}... "
if [ -x /ffp/bin/transmission-remote ]; then
/ffp/bin/transmission-remote -q
else
/bin/kill -9 $(pidof $command)
fi
echo "done."
else
echo "INFO: ${name} not running! Nothing to stop."
fi
_transmission_setup_softstop
echo "${name} stop sequence completed."
}

transmission_status()
{
_pids=$(pidof $name)
if test -n "$_pids"; then
echo "$name is running"
else
echo "$name not running"
fi
}

transmission_softstop_status()
{
if crontab -l grep -q 'softstop_off';
then
echo "DNS-323 will shutdown when there are no active downloads."
else
if crontab -l grep -q 'softstop';
then
echo "${name} will stop when there are no active downloads."
else
echo "softstop has not been configured."
fi
fi
}

transmission_restart()
{
transmission_stop
echo "Waiting for old instance to shutdown before starting new daemon..."
while transmission_status grep -q "is running"
do
sleep 5
done
transmission_start
}

# this routine checks to see if transmission is still alive
# and if there are any downloads active.
# if no downloads are active, it attempts to stop transmission.
# if "off_when_done" option is specified, it will attempt to
# shutdown the DNS-323 as well.
_transmission_softstop()
{
echo -n "`date`: Checking downloads... "
if /ffp/bin/transmission-remote -l grep -q 'downloading';
then
echo "downloads are still active!"
else
echo "no active downloads found!"
transmission_stop
if [ "${1}" = "off_when_done" ]; then
echo "attempting to shutdown the DNS-323 now. Bye Bye!"
touch /tmp/shutdown
fi
fi
}

# this routine sets up softstop as a cron job that runs every half hour
# to ADD the cron job, pass "stop_when_done" into the routine,
# to ADD the cron job with shutdown option, pass "off_when_done",
# any other values (or no value) means REMOVE by default
_transmission_setup_softstop()
{
echo "Setting up softstop on crontab... "
crontab -l > ${TMP_CRONTAB}
sed -i -e '/transmission.sh softstop/d' ${TMP_CRONTAB}
TMP_NAME=""
case "$1" in
stop_when_done)
TMP_NAME="softstop"
echo "INFO: This job will stop $name when all downloads are completed."
;;
off_when_done)
TMP_NAME="softstop_off"
echo "INFO: This job will attempt to shutdown the DNS-323 when all downloads are completed."
;;
*donothing_when_done)
echo -n "Removing job from crontab... "
;;
esac
if [ "${TMP_NAME}" != "" ]; then
# sanity check - do not allow adding any job if transmission is not already running
if transmission_status grep -q "is running"; then
echo -n "Adding cron job to run every half hour... "
echo -e "0,30 * * * * /ffp/start/transmission.sh ${TMP_NAME} >> /ffp/log/${name}.cron.log 2>&1" >> ${TMP_CRONTAB}
sed -i -e '/^\s*$/d' ${TMP_CRONTAB}
else
echo "ERROR: ${name} is not running. NOT adding job to crontab."
fi
fi
crontab ${TMP_CRONTAB}
rm ${TMP_CRONTAB}
echo "done."
}


case "$1" in
stop)
transmission_stop
;;
softstop)
_transmission_softstop
;;
softstop_off)
_transmission_softstop off_when_done
;;
set_off_when_done)
_transmission_setup_softstop off_when_done
;;
set_stop_when_done)
_transmission_setup_softstop stop_when_done
;;
set_donothing_when_done)
_transmission_setup_softstop donothing_when_done
;;
softstop_status)
transmission_softstop_status
;;
restart)
transmission_restart
;;
status)
transmission_status
;;
start'')
transmission_start
;;
*)
echo "Usage: $0 startstoprestartstatusset_off_when_doneset_stop_when_doneset_donothing_when_donesoftstop_status"
;;
esac

2 comments:

Anonymous said...

Hi!

Thanks for this, been curious why my box get to sleep for just a bit of time, and then spins up again, found out that it`s because of transmission, and on the way to my solution I found this entry which does exactly what I need.
Thanks alot.

stylesuxx

Anonymous said...

Hi,

Thanks for the information on setting up the pacakges on the DNS-323.

I'm looking for a script to strip out as much as possible from memory that relates to Trasmission and Clutch (or associated packages) and then to start them up the first time I go back to the page.. Not sure if this is possible but am wondering if this scrpit will do that.