How to automatically wake from sleep?


Recommended Posts

Hi, I'm running an up-to-date version of unRAID (6.1.9, I believe), I have installed the Dynamix Sleep plugin, and I have enabled wake-on-lan on the motherboard.  My unRAID machine will now happily sleep after the disks spin down (per the Sleep plugin schedule) or if I use the 'Sleep' button from the web UI.  WoL is working perfectly.  What I haven't fathomed yet, is how to schedule the machine to wake up for various maintenance (e.g., I would like my Plex server to update the libraries every day at 3 AM -- but this currently won't happen when the machine is asleep).  I could set up a scheduled task on one of my Windows boxes to send the WoL command, but it seems rather inelegant to have to schedule one PC just to wake up another PC!  Can anyone offer some advice here as to how to make my unRAID machine wake up for maintenance at a given time every day?

 

Many thanks!

Link to comment
  • 9 months later...

This Script should do it.

Just paste it into S3 Sleep settings in the custom command box (post sleep)

Be sure you disabled resume by rtc alarm in bios and set the bios time to rtc time.

 

It will wakeup every day at 09.55. So you can set jobs for 10.00.

time=09:55
now=$(date +%s)
other=$(date -d $time +%s)
if [ $now -ge $other  ]
then
	echo `date '+%s' --date='tomorrow 09:55:00'` > /sys/class/rtc/rtc0/wakealarm
else
    echo `date '+%s' --date='today 09:55:00'` > /sys/class/rtc/rtc0/wakealarm
fi

 

 

 

 

Monday to Friday exclude Saturday and Sunday:

 

time=09:55
now=$(date +%s)
other=$(date -d $time +%s)
dayofweek=$(date +%u)
if [ $now -ge $other  ] && [ $dayofweek -lt 5 ]
then
echo `date '+%s' --date='tomorrow 09:55:00'` > /sys/class/rtc/rtc0/wakealarm
elif [ $dayofweek -ge 5 ]
then
    echo `date '+%s' --date='next monday 09:55:00'` > /sys/class/rtc/rtc0/wakealarm
else
    echo `date '+%s' --date='today 09:55:00'` > /sys/class/rtc/rtc0/wakealarm
fi

 

 

 

 

Edited by feraay
  • Like 1
Link to comment
  • 8 months later...
  • 4 months later...

sorry for late answer.

 

so you want the server to wake up every day at the same time?

 

 

 

time=09:55
now=$(date +%s)
other=$(date -d $time +%s)
if [ $now -ge $other  ]
then
	echo `date '+%s' --date='tomorrow 09:55:00'` > /sys/class/rtc/rtc0/wakealarm
else
    echo `date '+%s' --date='today 09:55:00'` > /sys/class/rtc/rtc0/wakealarm
fi

 

 

 

to be honest the better way to do it is set a alarm in bios and repeat every day.

 

 

the script is only needed if you don't want the server to wake up every day.

Edited by feraay
Link to comment
  • 1 year later...
On 4/7/2018 at 11:43 AM, feraay said:

sorry for late answer.

 

so you want the server to wake up every day at the same time?

 

 

 


time=09:55
now=$(date +%s)
other=$(date -d $time +%s)
if [ $now -ge $other  ]
then
	echo `date '+%s' --date='tomorrow 09:55:00'` > /sys/class/rtc/rtc0/wakealarm
else
    echo `date '+%s' --date='today 09:55:00'` > /sys/class/rtc/rtc0/wakealarm
fi

 

 

 

to be honest the better way to do it is set a alarm in bios and repeat every day.

 

 

the script is only needed if you don't want the server to wake up every day.

Hi.

I would like to wake up my unraid every day. This is my script:

 

time=08:00
now=$(date +%s)
other=$(date -d $time +%s)
if [ $now -ge $other  ]
then
	echo `date '+%s' --date='tomorrow 08:00:00'` > /sys/class/rtc/rtc0/wakealarm
else
    echo `date '+%s' --date='today 08:00:00'` > /sys/class/rtc/rtc0/wakealarm
fi

It always works only on the first day. On the 2nd day the server stays in sleep mode. It seems that the alarm is not reset.

Can somebody help me with it? Thank you.

Link to comment
  • 7 months later...

It doesn't work at all.

 

Getting it to sleep manually and with Dynamix Sleep automatically on schedule works great.

 

But waking up doesn't work really. It wakes up, when I send a WOL signal from another PC or router, but not with this script. 

 

-Time is set correctly in the settings. 

-The script above:

On 4/7/2018 at 11:43 AM, feraay said:

 


time=09:55
now=$(date +%s)
other=$(date -d $time +%s)
if [ $now -ge $other  ]
then
	echo `date '+%s' --date='tomorrow 09:55:00'` > /sys/class/rtc/rtc0/wakealarm
else
    echo `date '+%s' --date='today 09:55:00'` > /sys/class/rtc/rtc0/wakealarm
fi

 

 is modified for testing to wake up 2-3min. after manual sleep, so I mean those time stamps (3 all in all ) are set with a different time stamps, 2 or 3 mins in the future.

 

Just for the safety of using it wrong: It should be put in the "Custom commands before sleep:"? Tried both, but it didn't work either way. 

 

EDIT: AWW... Seems like it needs to shut down by itself (not manually) in order to run the script. I'm stupid, but tbh didn't thought this condition needs to be like that. I thought, that for the system, it is the same.

 

But still one question: In which cistom comand box should it go: "Custom commands before sleep:" or "Custom commands after wake-up:" or both?

Edited by Kazino43
Link to comment
  • 4 months later...

I wonder how Synology is able to wakeup when receiving things like SMB request, I can only think of basic packet detection done with some BMC interface.

For my situation I assigned an old rpi to send magic packets to the Unraid on known clients ping status 0. as the operating times vary.

a.t.m I have this current cron job for testing:

#!/bin/bash

# 20200607
# sudo crontab -e
# sudo apt-get install etherwake
# sudo apt-get install fping

# Config
MACADDR[0]="AA:BB:CC:DD:EE:FF"
#MACADDR[1]="AA:BB:CC:DD:EE:FF"
IPHOSTS[0]="x.x.x.x"
#IPHOSTS[1]="x.x.x.x"
IPCLIENS[0]="x.x.x.x"
IPCLIENS[1]="x.x.x.x"
NICID="ethx"


# Wakeup
wakeupDevice(){
    printf "\nSending Magic WOL packet with: \n\tsudo etherwake $1 -i $2\n"
    sudo etherwake "$1" -i "$2"
}

triglog="/var/log/cron_triglog.log"

lenh=${#MACADDR[@]}
lenw=${#IPHOSTS[@]}
if [ $lenh != $lenw ]; then
    echo "[$timestamp] Error: MACADDR and IPHOSTS array do not match in lenght, check config..." > $triglog
    exit 1
fi

# Monitor every ~10 seconds for status
i=0
while [ $i -lt 6 ]; do
    # NOTE: Use absulute paths!
	timestamp=$(date +%s)
	echo "$timestamp"

    hostsonline=false
    requestwol=false

    # Ping hosts
    for hip in ${IPHOSTS[*]}; do
        fping -c1 -t300 ${hip} 2>/dev/null 1>/dev/null
        if [ "$?" = 0 ]; then
            printf "Host %s found\n" ${hip}
            hostsonline=true
        else
            printf "Host %s not found\n" ${hip}
            hostsonline=false
        fi
    done

    # Ping clients if needed
    if [ ${hostsonline} = true ]; then
        printf "All hosts replied\n"
    else
        for cip in ${IPCLIENS[*]}; do
            fping -c1 -t300 ${cip} 2>/dev/null 1>/dev/null
            if [ "$?" = 0 ]; then
                printf "Client %s found\n" ${cip}
                requestwol=true
            else
                printf "Client %s not found\n" ${cip}
            fi
        done
    fi

    # Send WOL
    if [[ ${requestwol} = true && ${hostsonline} = false ]]; then
        echo "[$timestamp] WOL: Sending Magic WOL packet using ${MACADDR[$i]} - ${NICID}" >> $triglog
        for (( i=0; i<$lenw; i++ )); do
            wakeupDevice ${MACADDR[$i]} ${NICID}
        done
    fi

    sleep 9 # ~6 sec of tollerance, consider https://mmonit.com to manage jobs below 1 minute
    i=$(( i + 1 ))
done

 

Edited by 54tgedrg45
Link to comment
  • 5 months later...

Hello i need some help with this script i want to know what would i need to change to get my server to wake from sleep at 11:00PM every Saturday :)

time=09:55

now=$(date +%s)

other=$(date -d $time +%s)

if [ $now -ge $other ]

then

echo `date '+%s' --date='tomorrow 09:55:00'` > /sys/class/rtc/rtc0/wakealarm

else

echo `date '+%s' --date='today 09:55:00'` > /sys/class/rtc/rtc0/wakealarm

fi 

Link to comment
  • 3 months later...
On 6/12/2019 at 11:55 PM, OOmatrixOO said:

It always works only on the first day. On the 2nd day the server stays in sleep mode. It seems that the alarm is not reset.

Can somebody help me with it? Thank you.

 

I know that this is an old post, but for everyone who still has the same problem of rtc working on the 1st day and not the second, this is my version of the code:

 

echo 0 > /sys/class/rtc/rtc0/wakealarm

time=5:58
now=$(date +%s)
other=$(date -d $time +%s)
if [ $now -ge $other  ]
then
	echo `date '+%s' --date='tomorrow 5:58:00'` > /sys/class/rtc/rtc0/wakealarm
else
    echo `date '+%s' --date='today 5:58:00'` > /sys/class/rtc/rtc0/wakealarm
fi

 

This resets the rtc clock which will make the code work every day.

echo 0 > /sys/class/rtc/rtc0/wakealarm

 

I use this code because the rtc function in my bios doesn't wake UNRAID from sleep. But this code worked a treat! If you wish to change the time, change all of the time values to your desired time. This code also has to be placed in the 'custom commands before sleep' text box.

Edited by corfoto4
  • Like 1
Link to comment
  • 1 month later...
  • 4 months later...
  • 1 year later...
On 4/7/2018 at 11:43 AM, feraay said:

the script is only needed if you don't want the server to wake up every day.

I do want the server to wake up every day, sadly the wake up timer in my bios does not seem to work as the system stays in sleep. 

gonna try this script now. thanks! 

Edited by cholzer
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.