Using Linux RTC for Wake-Up Functionality

In Linux systems, we can use crontab to set periodic tasks, such as scheduled shutdowns, but how do we schedule wake-ups? After 2000, many computer motherboards have supported the RTC (real-time clock) functionality. If the corresponding BIOS program supports the relevant settings, then it can be configured in the BIOS. If not, or if you prefer not to use the BIOS settings, you can directly set the RTC Alarm Clock through Linux.BIOS Wake-Up SettingsIn many computer motherboards’ BIOS programs, there is an option in the Power Management Settings to set a wake-up alarm, which is managed by APM or ACPI.

APM: Advanced Power Management, is an older power management standard;

ACPI: Advanced Configuration and Power Interface, is the newer, more advanced standard.

The wake-up settings configured in many motherboard BIOS only generate a wake-up signal once at the set time each day, and cannot achieve wake-ups only on weekdays while not waking up on weekends. Therefore, we disable the relevant settings in the BIOS and set it directly through Linux.Direct Linux ConfigurationIn the Linux system, first confirm that the Kernel configuration has the following RTC-related configuration items enabled (for example, kernel version 3.2.0-23):

$ 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" 

Once you confirm the presence of the following configuration items, you can proceed with the following simple settings:

cat /sys/class/rtc/rtc0/wakealarm

If this command returns no value, it indicates that no RTC alarm has been set. Next, we will clear the RTC alarm and set it to generate a wake-up signal 3 minutes later:

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

After setting, using the previous cat command to confirm will return a value like1354037019, which is the Unix epoch time, representing the number of seconds since midnight Coordinated Universal Time (UTC) on January 1, 1970 (the returned value will vary based on the current time).After executing the above commands, we can shut down the computer, and it will automatically power on after 3 seconds.We want to achieve a shutdown at 00:05 every weekday (Monday to Friday) and then wake up after 420 minutes (7 hours). Therefore, we will create a shutwake script in the /usr/local/bin directory with the following content:

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

This script sets the RTC alarm wake-up time for 420 minutes later and shuts down the computer.Next, we will edit the cron scheduled task configuration file using the crontab -e command, with the following relevant configuration information:

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

From this configuration, we can see: m is 05, indicating 05 minutes; h is 00, indicating 00 hours; dom is *, indicating every day; mon is *, indicating every month; dow is 1-5, indicating Monday to Friday; command is /usr/local/sbin/shutwake, indicating the command/script to be executed. It is clear that the shutwake script will be executed at 00:05 every Monday to Friday.

Leave a Comment