Buffalo NAS-Central Forums
http://forum.buffalo.nas-central.org/

Custom Sleep Mode for Linkstation Duo
http://forum.buffalo.nas-central.org/viewtopic.php?f=37&t=21982
Page 1 of 1

Author:  brumi [ Fri Jul 16, 2010 5:08 pm ]
Post subject:  Custom Sleep Mode for Linkstation Duo

Hi all,

I've been playing around with my LS-WX2.0T for quite some time now. Overall I'm satisfied with the features and after opening up the box (as described on the wiki - worked great) and having nzbget running there is not much missing. However, the provided mechanisms for standby/sleep are far from being useful for me.

The auto-function is not really an option for me, since I'm also using the Linkstation for streaming media to my PS3. Keeping a computer running to prevent the Linkstation from going to sleep when I'm watching movies is not what I want. The sleep-timer is also a bit inflexible for my needs; when doing downloads over night, I want my Linkstation to stay on for that purpose.

So I started making a cronjob to send the Linkstation into sleep-mode whenever it is not accessed for a certain amount of time. I do this by periodically checking the open connections and when no connections have been found, the script puts the Linkstation into sleep-mode. For those who have similar needs fell free to use the script below.
Code:
#!/bin/sh

################################################################################
#
# CUSTOM_SLEEP                                               by brumi 2010-07-16
#
################################################################################

# This cron-job is intended to be used as replacement for the provided standby-
# mechanism from buffalo. It checks for open connections and only when there are
# no open connections found for a certain period of time it will put the link-
# station into standby.

# TODO: get WOL working with this script
# TODO: parametrize deamon-list (see nzbget further down)


TIMEOUT=1800        # wait TIMEOUT seconds before going to standby

CONN_FILE=/var/run/connected        # file to store timestamp
LOG_FILE=/var/log/custom_sleep        # log-file
ENABLE_LOG='no'                # enable logging [yes|no]


# ip addresses to exclude from the connection-check (i.e., local ones)
OWN_IPADDR=`/bin/ipaddr show eth1 | /bin/grep -o -e "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | /bin/sed s/"inet "//`
EXCLUDE_IPADDR=(${OWN_IPADDR} 0.0.0.0 127.0.0.1 239.255.255.250)
EXCLUDE_IPADDR_CT=4


# do nothing if already sleeping
if [ -f /etc/linkstation_standby ]; then
        exit 0
fi


# function to check if any clients are connected
connected () {
        CONN_IPADDR=`/bin/netstat -utna 2> /dev/null | /bin/sed s/"[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"/"&\n"/ | /bin/grep -o -e "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"`
        for ip in $CONN_IPADDR
        do
                exclude=0
                for ((i=0;i<$EXCLUDE_IPADDR_CT;i++))
                do
                        if [ ${EXCLUDE_IPADDR[$i]} == $ip ]; then
                                exclude=1
                        fi
                done
                if [ $exclude -eq 0 ]; then
                        return 1
                fi
        done
        return 0
}

# check for connections and update $CONN_FILE
connected
if [ $? -eq 1 ]; then
        if [ -f $CONN_FILE ]; then
                rm $CONN_FILE
        fi
else
        if [ ! -f $CONN_FILE ]; then
                date +%s > $CONN_FILE       
        fi
fi       


# check when last connection was found
if [ -f $CONN_FILE ]; then
        last_conn=`/bin/cat $CONN_FILE`
        comp_time=`/bin/date +%s`
        let "comp_time -= $TIMEOUT"
        if [ $last_conn  -gt $comp_time ]; then
                if [ $ENABLE_LOG == 'yes' ]; then
                        echo -n `date` >> $LOG_FILE
                        echo ": custom_sleep: wait-after-connection prevented standby" >> $LOG_FILE
                fi
                exit 0
        fi
else
        if [ $ENABLE_LOG == 'yes' ]; then
                echo -n `date` >> $LOG_FILE
                echo ": custom_sleep: connections prevented standby" >> $LOG_FILE
        fi
        exit 0
fi

# check for nzbget deamon (don't interrupt post-processing)
if [ `/bin/ps | /bin/grep nzbget | /usr/bin/wc -l` -gt 0 ]; then
        if [ $ENABLE_LOG == 'yes' ]; then
                echo -n `date` >> $LOG_FILE
                echo ": custom_sleep: nzbget prevented standby" >> $LOG_FILE
        fi
        exit 0
fi

# no one busy -> clear timestamp and STANDBY
if [ $ENABLE_LOG == 'yes' ]; then
        echo -n `date` >> $LOG_FILE
        echo ": custom_sleep: going to standby" >> $LOG_FILE
fi
rm $CONN_FILE
/usr/local/sbin/PowerSave.sh standby


Just install an appropriate cron-job to launch the script on a regular basis (e.g., every 15 minutes). For me this approach works very well. As soon as the Linkstation is started and used it keeps on running, after 30 minutes of inactivity it goes to sleep.

One thing that's suboptimal with that solution is that the Linkstation does not react on WOL-packets. To wake it up, the function-button needs to be pressed. As far as I have seen, the Linkstation uses different commands to go to sleep when auto-mode is enabled. At the moment I have no time messing around with all the involved settings/scripts. For now I'm happy with how the box is doing.

I hope this is of some use for anyone out there.

Author:  tommac [ Fri Aug 20, 2010 11:49 am ]
Post subject:  Re: Custom Sleep Mode for Linkstation Duo

Is it possible to use this also on Linkstation Pro LS-GL?

How to install?
How to set up this for the cron deamon?

Author:  brumi [ Sat Aug 21, 2010 10:35 am ]
Post subject:  Re: Custom Sleep Mode for Linkstation Duo

tommac wrote:
Is it possible to use this also on Linkstation Pro LS-GL?

I can't tell whether this works on the Pro LS-G or not. If it uses the same firmware (this script works on FW 1.24 up to 1.33 on the LS-WX) it should be no problem. But just try and let us know.

tommac wrote:
How to install?
How to set up this for the cron deamon?

A short description for setting everything up:
  • Copy the code into a textfile (e.g. custom_sleep.sh)
  • Move this file onto your LS
  • Telnet/SSH to your LS (you need root-access)
  • Move the file into the /etc/cron/cron.d/ directory, e.g.
    Code:
    mv /mnt/array1/share/custom_sleep.sh /etc/cron/cron.d/
  • Change owner to root and allow execution of this file
    Code:
    chown root:root /etc/corn/cron.d/custom_sleep.sh
    chmod a+x /etc/cron/cron.d/custom_sleep.sh
  • Add the following line into /etc/cron/crontabs/root:
    Code:
    */5 * * * * /etc/cron/cron.d/custom_sleep.sh
  • Restart cron-deamon with
    Code:
    /etc/init.d/cron.sh restart
  • Check if the new job is listed via the command "crontab -l". If everything is o.k. you should get some output like below
    Code:
    root@LinkStation:~# crontab -l
    00 0 * * * /etc/cron/cron.d/mailnotice.sh
    05 4 * * * /etc/cron/cron.d/logrotate -f
    */5 * * * * /etc/cron/cron.d/logrotate
    08 4 * * * /etc/cron/cron.d/calib_time.sh
    00 3 * * * /etc/cron/cron.d/date_to_lcd
    */30 * * * * /etc/cron/cron.d/ms_checkversion.sh
    00 2 * * sun /etc/cron/cron.d/mdscan start date
    */5 * * * * /etc/cron/cron.d/custom_sleep.sh

Hope this helps.

Author:  tommac [ Tue Aug 31, 2010 11:22 am ]
Post subject:  Re: Custom Sleep Mode for Linkstation Duo

brumi wrote:
I can't tell whether this works on the Pro LS-G or not. If it uses the same firmware (this script works on FW 1.24 up to 1.33 on the LS-WX) it should be no problem. But just try and let us know.

I just created a script (ping solution) on my own, because varios commands (e.g. netstat) does not exist on my Linkstation Pro LS-GL.

I created a new thread in the LS Pro/ Live area here: http://forum.buffalo.nas-central.org/viewtopic.php?f=39&t=22155&start=0

Thank you for your asssitence until now.

Regards, tommac

Author:  philrou [ Fri Sep 24, 2010 6:42 pm ]
Post subject:  Re: Custom Sleep Mode for Linkstation Duo

hi, man i t try to figure out the buffalo LS Wol (WakeUpOnLAN) feature. Buffalo use port number 9 instead of the default port 7.

I found a tool wolcmd and wolcmdgui from depicus author (it's free)

with wolcmd you can create batch file
with

wolcmd 0024454567E4 xxx.xxx.xxx.xxx yyy.yyy.yyy.yyy p

xxx.xxx.xxx.xxx is the IP address (i.e. 192.168.1.31)
yyy.yyy.yyy.yyy is the subnet mask (i.e. 255.255.255.0)
p is port number (default to 7) buffalo use 9 instead of 7

it works fine

hope its helps you guys.

see you!!

Author:  scorpiorat11 [ Sun Aug 07, 2011 5:16 am ]
Post subject:  Re: Custom Sleep Mode for Linkstation Duo

Hi! I just purchased a Buffalo Linkstation LS-WXL/E-Ap, now refreshed with 1.52 firmware. I tried your script after enabling the ssh access, but it never succeeded. When the NAS was set at "Auto" mode, and no "NAS PM service" on windows clients were running, the NAS will only keep online for 5 minutes then go to sleep mode.

From your script, I can't find the mechanism which prevents the NAS from entering sleep mode after it's waken up by WOL, even when there're outer connections being detected.

Please comments.

Thanks!

Author:  kenatonline [ Sun Aug 07, 2011 1:49 pm ]
Post subject:  Re: Custom Sleep Mode for Linkstation Duo

scorpiorat11 wrote:
...
From your script, I can't find the mechanism which prevents the NAS from entering sleep mode after it's waken up by WOL, even when there're outer connections being detected.
...

???
brumi wrote:
...
One thing that's suboptimal with that solution is that the Linkstation does not react on WOL-packets. To wake it up, the function-button needs to be pressed.
...

What haven't you understand from what brumi wrote here in his first post?
You can not use WoL to wake up the box, when you use his script.

You can, of course, modify the script to work like you want it to work.
Just have a look at the following wiki page to get the idea how to keep
a Buffalo "X" or "V" box alive without a client sending WoL pakets:
http://buffalo.nas-central.org/wiki/Left_overs_for_%27X%27_and_%27V%27_series

Author:  scorpiorat11 [ Sun Aug 07, 2011 4:27 pm ]
Post subject:  Re: Custom Sleep Mode for Linkstation Duo

Quote:
The auto-function is not really an option for me, since I'm also using the Linkstation for streaming media to my PS3. Keeping a computer running to prevent the Linkstation from going to sleep when I'm watching movies is not what I want. The sleep-timer is also a bit inflexible for my needs; when doing downloads over night, I want my Linkstation to stay on for that purpose.


Quote:
What haven't you understand from what brumi wrote here in his first post?
You can not use WoL to wake up the box, when you use his script.


When I firstly read Brumi's statement above, I thought his codes will automatically prevent the NAS from going to sleep, given there's any client connection detected. But it's not true.

However, thank you for the information provided, I'll try to add the "pwrmgr -c localhost act" in the codes to see if it realy helps.

Thanks,

Author:  kenatonline [ Sun Aug 07, 2011 5:53 pm ]
Post subject:  Re: Custom Sleep Mode for Linkstation Duo

I wouldn't add the pwrmgr into the script, but use the
script I provided the wiki link for and integrate this.
Start the wiki script in init.d as a detached script.
When the script from this thread detects the absence
of any client, let it delete the "lock"-file from the wiki
script. The wiki script will then terminate itself after 180
seconds (at most).

Author:  AndreasFG [ Fri Jan 25, 2013 8:14 pm ]
Post subject:  Re: Custom Sleep Mode for Linkstation Duo

Below my contribution to all you guys who helped me getting this far as to wanting to improve the scripts above.

After the LS is woken up by a WOL (WakeupOnLAN) the LinkStation boots and cron starts to run this script up every 2 minutes. In the first part the script looks for a file in user storage called RunOnce.sh. If there s one it is excuted. This is my "backdoor" to regain root access "just in case" and is NOT needed to control shutdown. The 2nd part does that. It looks for activity. If it finds logged in users or network traffic it sends a request to the power manager to stay up another 3 minutes.

Code:
#!/bin/bash
# for Buffalo LS-WSXL and compatible                    /afg 2012-11-20
# -----------------------------------------------------------------------------
# cron-diven script to keep the box in 'AUTO'-mode running for as long as one
# of the enumerated processes is running (see "/-sh|sshd:|rsync/" below,) or
# the sum derived from all numeric fields for eth1 (packets + bytes + constants)
# has increased by 1000 during the last cron interval; or a Samba client has
# active shares.
# ------------------------------------------------------------------------------
# And, just in case, if there is a RunOnce.sh, then run it...
# ------------------------------------------------------------------------------
fN=/tmp/StayUp
rO=/mnt/array1/shared/_Backup/RunOnce
if [ -f $rO.sh ]; then  # RunOnce
  /bin/mv -f  $rO.sh $rO
  if [ $? -eq 0 ]; then
    /bin/chmod +x $rO
    echo "$rO.sh found and executed by
$0 on $(date)
-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
$(cat $rO)
-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
$($rO 2>&1)
-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
$rO.sh terminated with rc=$?
at $(uptime)" > $rO.log
    /usr/local/bin/sendmail.sh FuncRawData $rO.log
  fi
fi
/bin/touch $fN          # past eth1 activity
pN=$(cat $fN)
cN=$(ifconfig eth1 | sed -r 's/[^0-9]+/\n/g'|awk '{tot+=0+$1}END{print tot}')
if [ $(( cN - pN )) -lt 1000 ] && \
   [ -z $(/bin/ps|/usr/bin/awk '{if($5 ~ /-sh|sshd:|rsync/){print "1"}}') ] && \
   [ $(ps | grep smbd | wc -l) -lt 3 ]; then
  /bin/logger "$0 - no activity detected"
else
  /usr/local/sbin/pwrmgr -c localhost act
fi
echo $cN > $fN

Notes:
The last "if" decides whether to ask pwrmgr to stay up. They are evaluated in the order of least likely / least overhead: TCPIP traffic, user activity processes (rsync, cmd.shell and SAMBA users). It works well for me. Mine boots every night to catch the files changed on the ubuntu file server for which the LS is an instant and portable (hardware) backup server.

Author:  LesG [ Sun Dec 29, 2013 12:01 pm ]
Post subject:  Re: Custom Sleep Mode for Linkstation Duo

brumi wrote:
I hope this is of some use for anyone out there.


It was, thank you :up:

I used a combination of your script, plus kenatonline's posts/auto mode power management wiki to build my own solution.

I run my LS-WXL in auto mode so it responds to WOL packets, and I was able to mod your script to keep my NAS alive (using ' pwrmgr -c localhost act') if there where connections. I removed the nzbget check section entirely btw as, tbh, I've no idea what nzbget is or what uses it! :lol:

Author:  hongdat1106 [ Mon Jan 06, 2014 11:30 am ]
Post subject:  Re: Custom Sleep Mode for Linkstation Duo

LesG wrote:
brumi wrote:
I hope this is of some use for anyone out there.


It was, thank you :up:

I used a combination of your script, plus kenatonline's posts/auto mode power management wiki to build my own solution.

I run my LS-WXL in auto mode so it responds to WOL packets, and I was able to mod your script to keep my NAS alive (using ' pwrmgr -c localhost act') if there where connections. I removed the nzbget check section entirely btw as, tbh, I've no idea what nzbget is or what uses it! :lol:



dear friend,

i also used NAS LS-WVL as you, i want use "auto mod" but i can't write a script same you. Can you send for me your script?thank you so much

Author:  LesG [ Tue Jan 20, 2015 4:25 pm ]
Post subject:  Re: Custom Sleep Mode for Linkstation Duo

Code:

#!/bin/sh

################################################################################
#
# CUSTOM_SLEEP
#
################################################################################

# cron-job to check for connections and prevent 'auto-mode' sleep if found

TIMEOUT=240         # wait TIMEOUT seconds before going to standby

CONN_FILE=/var/run/connected      # file to store timestamp
LOG_FILE=/var/log/custom_sleep      # log-file
ENABLE_LOG='no'                      # enable logging [yes|no]


# ip addresses to exclude from the connection-check (i.e., local ones)
OWN_IPADDR=`/bin/ipaddr show eth1 | /bin/grep -o -e "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | /bin/sed s/"inet "//`
EXCLUDE_IPADDR=(${OWN_IPADDR} 192.168.1.255 0.0.0.0 127.0.0.1 239.255.255.250)
EXCLUDE_IPADDR_CT=5

# do nothing if already sleeping
if [ -f /etc/linkstation_standby ]; then
        if [ $ENABLE_LOG == 'yes' ]; then
           echo -n `date` >> $LOG_FILE
           echo " : already asleep!" >> $LOG_FILE
        fi
        exit 0
fi


# function to check if any clients are connected
connected () {
        CONN_IPADDR=`/bin/netstat -utna 2> /dev/null | /bin/sed s/"[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"/"&\n"/ | /bin/grep -o -e "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"`
        for ip in $CONN_IPADDR
        do
                exclude=0
                for ((i=0;i<$EXCLUDE_IPADDR_CT;i++))
                do
                        if [ ${EXCLUDE_IPADDR[$i]} == $ip ]; then
                                exclude=1
                        fi
                done
                if [ $exclude -eq 0 ]; then
                        CONN_DEVICE=$ip
                        return 1
                fi
        done
        return 0
}

# function to request that NAS 'stay awake' for another 4 minutes
stay_awake () {
   pwrmgr -c localhost act
        if [ $ENABLE_LOG == 'yes' ]; then
           echo -n `date` >> $LOG_FILE
                echo " : stay awake requested" >> $LOG_FILE
        fi
        return 0
}

# check for connections and update $CONN_FILE
connected
if [ $? -eq 1 ]; then
        if [ -f $CONN_FILE ]; then
                rm $CONN_FILE
        fi
else
        if [ ! -f $CONN_FILE ]; then
                date +%s > $CONN_FILE       
        fi
fi       


# check when last connection was found
if [ -f $CONN_FILE ]; then
        last_conn=`/bin/cat $CONN_FILE`
        comp_time=`/bin/date +%s`
        let "comp_time -= $TIMEOUT"
       
        if [ $last_conn  -gt $comp_time ]; then
                if [ $ENABLE_LOG == 'yes' ]; then
                        echo -n `date` >> $LOG_FILE
                        echo " : wait-after-connection" >> $LOG_FILE
                fi
                stay_awake
                exit 0
        fi
else
        if [ $ENABLE_LOG == 'yes' ]; then
                echo -n `date` >> $LOG_FILE
                echo " :" $CONN_DEVICE "connected" >> $LOG_FILE
        fi
        stay_awake
        exit 0
fi

# no one busy -> clear timestamp and STANDBY
if [ $ENABLE_LOG == 'yes' ]; then
        echo -n `date` >> $LOG_FILE
        echo ": no reason to stay awake" >> $LOG_FILE
fi

rm $CONN_FILE

Author:  brumi [ Wed Jan 28, 2015 1:53 pm ]
Post subject:  Re: Custom Sleep Mode for Linkstation Duo

I haven't been here for quite a while - nice to see that my script was of some use to others. :)

Page 1 of 1 All times are UTC+01:00
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/