1

Good Afternoon,

I have switched from a Windows server to Ubuntu server and I have managed to replicate all the items I had on the old server with the exception of sleep mode.

I currently have Ubuntu Server 16.04.2 LTS installed and I simply cannot get it to wake using a crontab script.

using:

sudo rtcwake -u -s 120 - mem

makes the computer sleep for 2 minutes so I know the system is capable of sleeping and waking up, but trying transfer that to a script is difficult to me as it is all new and my knowledge of Ubuntu/Linux is very limited at the moment. so could you spare 2 minutes just to look over what I have done so far please.

The commands I have run so far are:

sudo crontab -e

then added this line at the end to test the feature out:

30 19 * * * /home/andy/suspend_until 19:45

In the suspend_until file is:

#!/bin/bash

# Auto suspend and wake-up script

# Argument check
if [ $# -lt 1 ]; then
    echo "Usage: suspend_until HH:MM"
    exit
fi

# Check whether specified time today or tomorrow
DESIRED=$((`date +%s -d "$1"`))
NOW=$((`date +%s`))
if [ $DESIRED -lt $NOW ]; then
    DESIRED=$((`date +%s -d "$1"` + 24*60*60))
fi

# Kill rtcwake if already running
sudo killall rtcwake

# Set RTC wakeup time
# N.B. change "mem" for the suspend option
# find this by "man rtcwake"
sudo rtcwake -l -m mem -t $DESIRED &

# feedback
echo "Suspending..."

# give rtcwake some time to make its stuff
sleep 2

# then suspend
# N.B. dont usually require this bit
#sudo pm-suspend

# Any commands you want to launch after wakeup can be placed here
# Remember: sudo may have expired by now

# Wake up with monitor enabled N.B. change "on" for "off" if 
# you want the monitor to be disabled on wake
xset dpms force on

# and a fresh console
clear
echo "Good morning!"

Credit to RedgeOnline

Then: chmod +x suspend_until

The server went into standby at 19:30 but never woke up.

Any help much appreciated

Thank you in advance for your time

Andy

Andy M
  • 11
  • 1
  • 3

2 Answers2

1

In your testing command:

sudo rtcwake -u -s 120 -m mem

the -u causes that rtcwake assumes, that your Computers clock (in BIOS) is set to UTC time. However, when you test for a relative time (120 seconds in this case) it does not really matter at what time the clock is set, so it works anyhow.

The script which is executed by cron uses the -l option, causing rtcwake to assume that your computers clock is set to local time.

In either case, you should be consistent, in which option you use and you should check your actual BIOS settings. To test rtcwake with an absolute time use the -t option:

sudo rtcwake -m mem -l -t $(date +%s -d ‘today 19:45’)

where I assumed that your computers clock is set to local time.

Cheers

romed
  • 141
  • 4
  • sudo rtcwake -m mem -l -t $(date +%s -d 19:45) works from the command line but how would i add that to crontab so that does it automatically – Andy M Jul 03 '17 at 21:18
  • I guess you could just abandon your script and put the _rtcwake_ command directly in the crontab. For example: `30 19 * * * /usr/bin/rtcwake -m mem -l -t $(date +%s -d 19:45) ` – romed Jul 04 '17 at 10:00
0

You could also suspend and wake your system using systemd, as described here: Scheduled suspend and resume with systemd

However, systemd is only available in ubuntu since version 15.04.

Cheers

romed
  • 141
  • 4