Linux.com

Home Learn Linux Linux Documentation Wake Up Linux With an RTC Alarm Clock

Wake Up Linux With an RTC Alarm Clock

Most Linux users know how to set scheduled automatic shutdowns using cron. Did you know you can also set automatic wakeups? Most motherboards built after 2000 support real-time clock (RTC) wakeups, so you can have your computer turn itself on and off on a schedule.

BIOS Wakeup

One way to wake up your computer at a scheduled time is to enter your computer's BIOS and set a wakeup alarm in the Power Management settings. This will be managed either by APM or ACPI settings, depending on the age of your BIOS and any modifications made by the motherboard manufacturer. APM, Advanced Power Management, is an older power management standard. ACPI, Advanced Configuration and Power Interface, is the newer, more advanced standard. Chances are you'll see both. Look for a setting to set the date and time for wakeups. If this does what you need, you're done and can go read something else now. On my main workstation it's limited and only schedules one wakeup event per day, and it won't let me schedule weekdays only. So this is a job for Linux itself.

 

time, time on linux, rtc alarm clock, scheduled Linux wakeupsJoseph Chamberlain Memorial Clock Tower, tallest clock tower in England. Image courtesy Wikimedia Commons

 

Kernel Support

If you have any scheduled wakeups set in your BIOS, remove them, and then verify your system has all the necessary pieces in place. This how-to is for kernel versions 2.6.22 and later; run uname -r to see your kernel version. Your Linux kernel should already have everything you need, unless you or your distribution maintainer have removed RTC support. So check your kernel configuration file, like this example:

$ grep -i rtc /boot/config-3.2.0-23-generic 
CONFIG_HPET_EMULATE_RTC=y
CONFIG_PM_TRACE_RTC=y
CONFIG_RTC_LIB=y
CONFIG_RTC_CLASS=y
CONFIG_RTC_HCTOSYS=y
CONFIG_RTC_HCTOSYS_DEVICE="rtc0"
[...]

That returns a couple dozen lines of output showing full RTC support. Another way is to check your system log. The syslog is configured a little differently on various distros, so one of these two examples should work:

# grep -i rtc /var/log/messages 
$ grep -i rtc /var/log/kern.log

And then you should see several lines of useful output like this:

 Nov 24 07:17:27 studio kernel: [0.248407] RTC time: 15:17:15, date: 11/24/12
Nov 24 07:17:27 studio kernel: [1.692667] rtc_cmos 00:03: RTC can wake from S4
Nov 24 07:17:27 studio kernel: [1.692762] rtc_cmos 00:03: rtc core: registered rtc_cmos as rtc0
Nov 24 07:17:27 studio kernel: [1.692789] rtc0: alarms up to one month, y3k, 114 bytes nvram, hpet irqs
Nov 24 07:17:27 studio kernel: [1.713143] rtc_cmos 00:03: setting system clock to 2012-11-24 15:17:17 UTC (1353943037)

This example shows that the RTC is set to Coordinated Universal Time (UTC), which is desirable because then you don't have to hassle with daylight savings time. (I love how we can change time itself, instead of adjusting our schedules.) rtc0 is the clock's device name, which is standard because it would be unusual to have more than one RTC.

Simple Wakeup Test

Now let's get to the fun part and do a simple manual wakeup test. First check if any wakeups are set:

$ cat /sys/class/rtc/rtc0/wakealarm

No value returned means no alarms are set. These two commands reset the alarm to zero, and then set a wakeup alarm three minutes in the future:

$ sudo sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm" 
$ sudo sh -c "echo `date '+%s' -d '+ 3 minutes'` > /sys/class/rtc/rtc0/wakealarm"

Now when you run cat /sys/class/rtc/rtc0/wakealarm you should see a value similar to 1354037019. This is the Unix epoch time, which is the number of seconds since UTC midnight 1 January 1970. You need to see a value here to verify that a wakeup time has been set. Next, shutdown your computer and wait for it to start. If this simple test succeeds you are ready to use this simple shutwake script to shutdown and start up your computer whenever you want:

#!/bin/bash 
sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm"
sh -c "echo `date '+%s' -d '+ 420 minutes'` > /sys/class/rtc/rtc0/wakealarm"
shutdown -h now

It works like this: make it executable, put it in root's path (like /usr/local/bin), and create a root cron job to run it when you want your computer to shut down, like this example that runs the script at five minutes past midnight on weeknights:

# crontab -e 
# m h dom mon dow command
05 00 * * 1-5 /usr/local/sbin/shutwake

The script will set the wakeup alarm at 420 minutes after shutdown at 12:05AM. This is a lot simpler than hassling with UTC and epoch time conversions, which is what you'll see in other RTC wakeup howtos. You can easily create multiple shutdown and wakeup times by creating different crontabs and modifying the number of minutes in the wakeup alarm.

RTC can be vexing, and there are a number of factors than can gum it up, so please check out the excellent MythTV ACPI Wakeup for troubleshooting various distros, and what to use for older kernels.

 

Comments

Subscribe to Comments Feed
  • David Said:

    I can just write zero to the wakealarm file. If I try to write a value > 0 it returns 'Permission denied'.

  • David Said:

    Ok, the permission was denied because only root was allowed to do this. It worked!

  • actionparsnip Said:

    If you use: echo value | sudo tee /path/to/thing you will be fine

  • Edson Said:

    Try: sudo sh -c "echo 'something' >> /etc/privilegedfile"

  • David Said:

    Thank you for the indications. I would like to add just that in the shutwake bash script I had to add the full paths to the commands.

  • f. fellini Said:

    I was wondering if it was possible to incorporate in the suspend script so that you can save battery life by waking from suspend to memory then suspending to disk (whenever hibernate is functional). There are times when I have run down my battery not knowing that the laptop was stored away in suspend mode.

  • hermann.kerr Said:

    After a few hickups, I got it working. Thank you for the great and informative article.

  • hendro Said:

    I already love GNU/linux and its community 100% for the past 5 years and i am an average/power user. I switched to linux because I really wanted to learned more and have sense of continuing progress when using my computer. Nowadays I use Fedora, Debian, and LinuxMint. And this article gave me more enlightenment, because i thought it was not possible to wake up computer on its own! Thanks Carla.

  • rh Said:

    I am running into issues. I can echo the UTC time +3 into wakealarm and than if if cat wakealarm i get the time plus 3 minutes in the future. When i shutdown, I have to manually start my computer. What is odd is if I cat wakealarm after my comuter boots it get UTC time is a year in the future. :-/

  • Mark Said:

    and better yet...there already exists a tool for all this: rtcwake ! :) it's part of util-linux, so pretty sure already installed almost everywhere. just run man rtcwake and enjoy It already fixes many issues mentioned here: *let's you view/ schedule new alarm in pretty print format; *offers choices to -shutdown,suspend,hibernate..; *solves problems with UTC/localtime..

Upcoming Linux Foundation Courses

  1. LFS230 Linux Network Management
    06 Oct » 09 Oct - Virtual
    Details
  2. LFS416 Linux Security
    06 Oct » 09 Oct - Washington
    Details
  3. LFD331 Developing Linux Device Drivers
    13 Oct » 17 Oct - Virtual
    Details

View All Upcoming Courses

Become an Individual Member
Check out the Friday Funnies

Sign Up For the Linux.com Newsletter


Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board