Scripts

This code is used as the startup script for the mlnet which is installed in the chrooted debian on the synology box. I'm not sure now from where the inspiration came. Maybe from the mldonkey wiki pages. So all my startup scripts follow this layout you see below.
I have added the softstop switch into the script. The softstop is used to shutdown mlnet only in case it is not downloading files. The softstop switch is invoked in the crontab. The purpose of removing the softstop script from the crontab after stopping the mlnet is to get the disk in the synology box to hybernate. The softstop check is added every time the mlnet is started (through the startup script only!).

PATH='/opt/bin:/opt/sbin:/usr/sbin:/bin:/usr/bin:/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/syno/bin:/usr/syno/sbin:/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/syno/bin:/usr/syno/sbin:/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/syno/bin:/usr/syno/sbin:/usr/local/bin:/usr/local/sbin'
# Location of the chroot
CHROOT=/volume1/public/debian/chroottarget
# Now on to usual bussines
MLNET=/usr/bin/mlnet
NAME=S80mlnet
DESC=mlnet
RUNMLNET=no
STARTDIR=/root/.mldonkey
PIDFILE=/root/.mldonkey/mlnet.pid
LOGFILE=/var/www/mldonkey.log
test -x $DAEMON || exit 0
# Include mlnet defaults if available
if [ -f /opt/etc/default/$NAME ] ; then
    . /opt/etc/default/$NAME
fi
if [ "$RUNMLNET" != "yes" ];then
  echo "$NAME not to be started. Edit /opt/etc/default/$NAME first."
  exit 1
fi
set -e
case "$1" in
  start)
    ps > "$NAME.tmp"
    if grep -c "$MLNET" "$NAME.tmp"
    then
      echo -n "$DESC already running: "
    else 
      echo -n "Starting $DESC: "
      chroot $CHROOT /bin/bash -c "cd $STARTDIR; $MLNET > $LOGFILE 2>&1 &"

In the code below the mldonkey startup script with the interval of 1 hour is inserted into the crontab of the current user. This means that every hour the mlnet is inspected if it is still downloading. If there is no downloading going on the mlnet will be stopped. You should adjust this time interval to your needs if there is some ratio out there that you have to keep.
# add the softstop routine to crontab
      sed -i -e '/S80mlnet softstop$/d' /opt/etc/crontab
      echo -e "0\t*\t*\t*\t*\troot\t/opt/etc/init.d/S80mlnet softstop" >> /opt/etc/crontab
      sed -i -e '/^\s*$/d' /opt/etc/crontab
      crontab /opt/etc/crontab
    fi
    echo "$NAME."
    rm "$NAME.tmp"
  ;;
  softstop)
    echo -n "Checking downloads "
    wget -O "$NAME.tmp" -q "http://admin:password@127.0.0.1:4080/submit?q=vd"
    if grep -q '<td class="dl al np">R</td>' "$NAME.tmp"
    then
      echo "and can't stop because downloads are running !"
    else
      echo -n "and stopping $DESC: "
      cat $CHROOT$PIDFILE|xargs kill
      rm $CHROOT$PIDFILE
      echo "$NAME."

In this part when it is clear that no files are being downloaded by the mlnet the crontab entry is removed for softstop (all of them, shoud there be more than one)
# remove the softstop routine invokation
      sed -i -e '/S80mlnet softstop$/d' /opt/etc/crontab
      crontab /opt/etc/crontab
    fi
    rm "$NAME.tmp"
  ;;
  stop)
    echo -n "Stopping $DESC: "
    cat $CHROOT$PIDFILE|xargs kill
    rm $CHROOT$PIDFILE
    echo "$NAME."
    # remove the softstop routine invokation
    sed -i -e '/S80mlnet softstop$/d' /opt/etc/crontab
    crontab /opt/etc/crontab
  ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    cat $CHROOT$PIDFILE|xargs kill
    rm $CHROOT$PIDFILE
    sleep 1
    chroot $CHROOT /bin/bash -c "cd $STARTDIR; $MLNET > $LOGFILE 2>&1 &"
    echo "$NAME."
  ;;
  *)
    N=/opt/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
  ;;
esac
exit 0
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.