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 

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:

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.



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..

  • ck Said:

    Wow. And you learn something new every day. It works. Made my day ^_^

  • alfred Said:

    Very nice. Unfortunately this method is far from ideal. What if I just shutdown my computer (APCI or soft one)? RTC wakup alarm is not set in such a case. It is much better to rely on cron for just shutting down and call wakeup script somewhere from within shutdown scripts. Each distro has such. I use Slackware and it's /etc/rc.d/rc.local_shutdown.

  • Dimitri Said:

    Hi, I followed your Tutorial but i have a problem .. I'm trying to run the script in a cron job but it doesn't work for me ... I putted the file in my home directory not in the directory you said (tried also but didn't work).. I can ran another script with Cron but this one doesn't do anything it runs manually with sudo ./shutwake

Upcoming Linux Foundation Courses

  1. LFD312 Developing Applications For Linux
    16 Feb » 20 Feb - Atlanta - GA
  2. LFD331 Developing Linux Device Drivers
    16 Feb » 20 Feb - San Jose - CA
  3. LFS220 Linux System Administration
    16 Feb » 19 Feb - Virtual

View All Upcoming Courses

Become an Individual Member
Check out the Friday Funnies

Sign Up For the 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