Automatically sleep and wake-up at specific times

77,897

Solution 1

rtcwake

The command that you are interested in is rtcwake:

This program is used to enter a system sleep state until specified wakeup time.

testing

To find the correct syntax that works for you try the following:

sudo rtcwake -u -s 60 -m mem

This should suspend the computer for 60 seconds before restoring. The significant parameter is mem You have several options you can choose - play to find the value that works best for you:

          standby
                 ACPI state S1. This state offers  minimal,  though  real,
                 power savings, while providing a very low-latency transi‐
                 tion back to a working system. This is the default mode.

          mem    ACPI state S3 (Suspend-to-RAM). This state offers signif‐
                 icant  power  savings  as everything in the system is put
                 into a low-power  state,  except  for  memory,  which  is
                 placed in self-refresh mode to retain its contents.

          disk   ACPI  state  S4  (Suspend-to-disk). This state offers the
                 greatest power savings, and  can  be  used  even  in  the
                 absence  of  low-level platform support for power manage‐
                 ment. This state operates  similarly  to  Suspend-to-RAM,
                 but  includes  a final step of writing memory contents to
                 disk.

          off    ACPI  state  S5  (Poweroff).  This  is  done  by  calling
                 '/sbin/shutdown'.   Not officially supported by ACPI, but
                 usually working.

          no     Don't suspend. The rtcwake command sets RTC  wakeup  time
                 only.

          on     Don't  suspend,  but  read  RTC  device  until alarm time
                 appears. This mode is useful for debugging.

suspend until a known time

A script (at the bottom of this post) could be used to suspend your computer and wake at a specific time:

syntax is suspend_until [hh:mm] for example

sudo ./suspend_until 07:30

Save the script as the name suspend_until and give it execute rights i.e.

chmod +x suspend_until

Cron

You can create a root cron job that calls this script to execute at a specific time in the evening and then awake in the morning:

sudo crontab -e

Now enter something like to run the suspend script at 23:30:

30 23 * * * /home/myhomefolder/suspend_until 07:30

suspend_until script

#!/bin/bash

# Auto suspend and wake-up script
#
# Puts the computer on standby and automatically wakes it up at specified time
#
# Written by Romke van der Meulen <[email protected]>
# Minor mods fossfreedom for AskUbuntu
#
# Takes a 24hour time HH:MM as its argument
# Example:
# suspend_until 9:30
# suspend_until 18:45

# ------------------------------------------------------
# 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!"

N.B.

Change mem in this part of the script for whatever suspend method works for you:

# Set RTC wakeup time
sudo rtcwake -l -m mem -t $DESIRED &

You may also have to substitute the -u flag in place of the -l flag depending on whether your hardware clock uses UTC (-u) or local (-l) time. Note that your hardware clock is different from the system clock you see in your operating system.

credit to redgeonline

Solution 2

Using rtcwake I have created a simple bash script. It uses php to translate natural language into system time. For example:

  • sudo ./cu "tomorrow 9am"

  • sudo ./cu "next monday 3pm"

  • sudo ./cu "1 hour ago"

    rtcwake: time doesn't go backward

You can download it here.

#!/bin/bash
export sdate=$1

date=`/usr/bin/php << 'EOF'
<?php
date_default_timezone_set("Etc/GMT-2");
$date = strtotime(GETENV("sdate"));
echo "\r".$date;
EOF`

rtcwake -m mem -t $date
Share:
77,897

Related videos on Youtube

drnessie
Author by

drnessie

I code sometimes.

Updated on September 18, 2022

Comments

  • drnessie
    drnessie almost 2 years

    How can I hibernate or sleep my Ubuntu 10.10 desktop and have it "wake up" the next day?

    I have seen software that can do this on windows, so it can't be hard on Ubuntu!

  • drnessie
    drnessie almost 13 years
    what I asked for... and so much more! Thank you for reading in between the lines!
  • nilsonneto
    nilsonneto almost 13 years
    thanks - just slightly updated to highlight which bit of the script you need to change for whatever suspend method works for you.
  • unhammer
    unhammer over 11 years
    The killall is unnecessary, rtcwake doesn't run as a daemon, it simply writes a value to /sys/class/rtc/rtc0/wakealarm; running it again will write another value to that file. Also, remove the & at the end of rtcwake, it will exit when it's done. Then you can remove the sleep command. And, if you want to run other root commands in the script, why not run the whole thing sudo instead of individual commands there?
  • unhammer
    unhammer over 11 years
    Note that date -d already understands several such strings: cyberciti.biz/tips/…
  • gare
    gare over 10 years
    # my old laptop would not wake up using the script above . so this is what i did # root crontab 30 20 * * * /home/gare/Documents/scripts/suspend_10_hours.sh >> /home/gare/Documents/scripts/suspend.log ~$ more /home/gare/Documents/scripts/suspend_10_hours.sh #!/bin/bash # old laptop will not wake; so tell rtcwake to suspend for 10 hours # 10 hours * 60 mins * 60 secs =36000 sudo rtcwake -u -s 36000 -m mem
  • Todd Baur
    Todd Baur over 8 years
    " depending on whether your hardware clock uses UTC (-u) or local (-l) time" - It isn't directly obvious how to determine this. Although I suspect pretty much everyone except those in the UK / GMT will probably want to use -l. sudo hwclock --show won't obviously tell you, although I guess it will say UTC in their somewhere if you need to use UTC.