Monday, February 18, 2008

Enhanced mldonkey script for your DNS-323

I wasn't too happy with Andy's startup script because I found it alittle too simple and lacked robustness in the startup sequence as well as some of convenience functions like stopping and restarting the mldonkey processes.

I also wanted to incorporate a nifty feature (written by Bartik for his synology) that regularly checks mldonkey for active downloads. If not found, it stops mldonkey automatically. Along the same lines, I have expanded on this idea to optionally turn off the box when all the downloads are completed. To do that, start mldonkey normally and then run the command ./mlnet.sh set_off_when_done. If at anytime you change your mind, just run the command ./mlnet.sh set_stop_when_done to change back to the default behaviour (i.e. stop mldonkey only).

So here it is for the benefit of others. Note that the script assumes a setup identical to mine as documented here. You may need to make the necessary amendments if your setup is different.

You will definitely need to change the following in the script:
  • uid and gid of the account used to run mldonkey (currently not working)
  • password of the admin account used to access the mldonkey web interface


#!/bin/sh

###################################################################
# author: Sidney Chong (bfg100k[at]gmail[dot]com) #
# version: 0.2 #
# date: 18/02/2008 #
# #
# Version History #
# --------------- #
# v0.1 - expanded ShadowAndy(http://www.shadowandy.net) script #
# to be more robust on startup as well as added functions #
# to check status, stop, restart and auto-stop when #
# downloads are completed. #
# v0.2 - added option to shutdown the DNS-323 when downloads are #
# completed. By default, the script only stops the #
# mldonkey process when there are no more active downloads.#
# #
###################################################################

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

export MLDONKEY_DIR="/mnt/HD_a2/mldonkey"
export TMPDIR="/mnt/HD_a2/mldonkey/temp"

MLNETBINDIR="/mnt/HD_a2/fun_plug.d/bin"
MLNETLOG="/mnt/HD_a2/fun_plug.d/log/mlnet.log"
TMP_CRONTAB="/mnt/HD_a2/fun_plug.d/log/crontab.tmp"
TMP_DL_STATUS="/mnt/HD_a2/fun_plug.d/log/dl.status.tmp"
ADMIN_PSWD="_SET_YOUR_PSWD_HERE"

mlnet_start() {
echo "Starting mlnet startup sequence... "
#check if mlnet is already running. if yes, do nothing
if [ `mlnet_status` = "running" ]; then
echo "INFO: mldonkey already running! Nothing to do."
return
fi
#check for inproper clean up from the past
if [ -e ${MLDONKEY_DIR}/mlnet.pid ]; then
rm ${MLDONKEY_DIR}/mlnet.pid
fi

if [ -d "${MLDONKEY_DIR}" ]; then

else
echo "mldonkey directory (${MLDONKEY_DIR}) not found. Creating one..."
mkdir ${MLDONKEY_DIR}
#change ownership of the folder to the user account we will use to run mlnet
#chown 504:703 ${MLDONKEY_DIR}
fi
#chmod 664 ${MLDONKEY_DIR}/*
${MLNETBINDIR}/mlnet > ${MLNETLOG} 2>&1 &
#${MLNETBINDIR}/mlnet -run_as_useruid 504 > ${MLNETLOG} 2>&1 &
if [ -d "${MLDONKEY_DIR}/incoming" ]; then
chmod 777 ${MLDONKEY_DIR}/incoming/
chmod 777 ${MLDONKEY_DIR}/incoming/*
fi
_mlnet_setup_softstop stop_when_done
echo "mlnet startup sequence completed. Check ${MLNETLOG} for more details."
}

mlnet_stop() {
echo "Stopping mlnet... "
if [ `mlnet_status` = "running" ]; then
echo -n "Killing mlnet process... "
if [ -e ${MLDONKEY_DIR}/mlnet.pid ]; then
cat ${MLDONKEY_DIR}/mlnet.pidxargs kill
else
pidof mlnet xargs kill
fi
echo "done. All mlnet processes killed."
else
echo "INFO: mlnet not running! Nothing to kill."
fi
_mlnet_setup_softstop
echo "mlnet stop sequence completed."
}

# this routine checks to see if mlnet is still alive
# and if there are any downloads active.
# if no downloads are active, it attempts to stop mlnet.
# if "off_when_done" option is specified, it will attempt to
# shutdown the DNS-323 as well.
_mlnet_softstop() {
echo -n "`date`: Checking downloads... "
#touch ${TMP_DL_STATUS}
wget -O ${TMP_DL_STATUS} -q "http://admin:${ADMIN_PSWD}@127.0.0.1:4080/submit?q=vd"
if grep -q 'R' ${TMP_DL_STATUS}
then
echo "downloads are still active!"
else
echo "no active downloads found!"
mlnet_stop
if [ "${1}" = "off_when_done" ]; then
echo "attempting to shutdown the DNS-323 now. Bye Bye!"
touch /tmp/shutdown
fi
fi
rm ${TMP_DL_STATUS}
}

# this routine sets up softstop as a cron job that runs every half hour
# to ADD the cron job, pass "add" 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
_mlnet_setup_softstop() {
echo "Setting up softstop on crontab... "
crontab -l > ${TMP_CRONTAB}
sed -i -e '/mlnet.sh softstop/d' ${TMP_CRONTAB}
TMP_NAME=""
case "$1" in
stop_when_done)
TMP_NAME="softstop"
echo "INFO: This job will stop mldonkey 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."
;;
*)
echo -n "Removing job from crontab... "
;;
esac
if [ "${TMP_NAME}" != "" ]; then
# sanity check - do not allow adding any job if mlnet is not already running
if [ `mlnet_status` = "running" ]; then
echo -n "Adding cron job to run every half hour... "
echo -e "0,30 * * * * /mnt/HD_a2/fun_plug.d/start/mlnet.sh ${TMP_NAME} >> /mnt/HD_a2/fun_plug.d/log/mlnet.log 2>&1" >> ${TMP_CRONTAB}
sed -i -e '/^\s*$/d' ${TMP_CRONTAB}
else
echo "ERROR: mlnet is not running. NOT adding job to crontab."
fi
fi
crontab ${TMP_CRONTAB}
rm ${TMP_CRONTAB}
echo "done."
}

mlnet_restart() {
mlnet_stop
echo "Waiting 10 secs before starting daemon..."
sleep 10
mlnet_start
}

mlnet_status() {
if [ -n "$(pidof mlnet)" ]; then
echo "running"
else
echo "stopped"
fi
}

case "$1" in
stop)
mlnet_stop
;;
softstop)
_mlnet_softstop
;;
softstop_off)
_mlnet_softstop off_when_done
;;
set_off_when_done)
_mlnet_setup_softstop off_when_done
;;
set_stop_when_done)
_mlnet_setup_softstop stop_when_done
;;
restart)
mlnet_restart
;;
status)
mlnet_status
;;
start'')
mlnet_start
;;
*)
echo "Usage: $0 startstoprestartstatusset_off_when_doneset_stop_when_done"
;;
esac

No comments: