How the set RTC as wake up source and set a specific time on all the days using sys file system
Categories:
Setting RTC as Wake-up Source for Scheduled System Power-on

Learn how to configure your Linux system, specifically on Sabre platforms, to use the Real-Time Clock (RTC) as a wake-up source, enabling automatic power-on at a specific time each day using the sys filesystem.
The Real-Time Clock (RTC) is a crucial component in many embedded systems and personal computers, responsible for keeping track of time even when the main system power is off. Beyond just timekeeping, the RTC often provides alarm capabilities that can be used to wake up a suspended or powered-off system. This article focuses on how to leverage the RTC as a wake-up source on Linux systems, particularly those based on the Sabre platform, by interacting with the /sys
filesystem. We'll cover the necessary steps to enable this feature and configure a daily wake-up time.
Understanding RTC Wake-up Mechanism
The Linux kernel exposes RTC functionalities through the /sys
filesystem, allowing user-space applications to query and configure RTC properties. For wake-up events, the key interface is typically /sys/class/rtc/rtc0/wakealarm
. When a time is written to this file, the RTC hardware is programmed to generate an interrupt at the specified time. If the system is in a low-power state (like suspend-to-RAM or powered off, assuming ACPI/BIOS support), this interrupt can trigger a system wake-up. This method is highly reliable as it relies on dedicated hardware.
flowchart TD A[System Powered Off/Suspended] --> B{RTC Alarm Set?} B -->|No| C[System Remains Off] B -->|Yes| D[RTC Counts Down] D --> E{Alarm Time Reached} E -->|Yes| F[RTC Generates Interrupt] F --> G[System Wakes Up] G --> H[Linux Kernel Boots/Resumes]
RTC Wake-up Process Flow
Identifying Your RTC Device
Before you can configure the RTC, you need to identify the correct RTC device. Most Linux systems will have at least one RTC, usually named rtc0
. You can verify its presence and capabilities by listing the contents of the /sys/class/rtc/
directory. The wakealarm
file is the primary interface for setting wake-up alarms.
ls -l /sys/class/rtc/rtc0/
cat /sys/class/rtc/rtc0/wakealarm
Checking RTC device and current wakealarm status
Setting a Daily Wake-up Time
To set a daily wake-up time, you need to write the desired alarm time to the wakealarm
file. The format for this file is a Unix timestamp (seconds since epoch). The RTC hardware will then compare its current time with this timestamp. For a daily wake-up, you'll typically calculate the timestamp for the desired time on the next day. This process needs to be repeated after each wake-up or system boot to ensure the alarm is set for the subsequent day.
# Example: Set wake-up for 07:00 AM tomorrow
# 1. Clear any existing wakealarm
echo 0 > /sys/class/rtc/rtc0/wakealarm
# 2. Get current time in Unix timestamp
CURRENT_TIME=$(date +%s)
# 3. Calculate tomorrow's 07:00 AM timestamp
# (Current day's 07:00 AM + 24 hours)
# First, get today's 07:00 AM timestamp
TODAY_7AM_TIMESTAMP=$(date -d "$(date +%Y-%m-%d) 07:00:00" +%s)
# If current time is already past 07:00 AM, set for tomorrow. Otherwise, set for today's 07:00 AM.
if [ "$CURRENT_TIME" -ge "$TODAY_7AM_TIMESTAMP" ]; then
WAKEUP_TIMESTAMP=$((TODAY_7AM_TIMESTAMP + 24*60*60))
else
WAKEUP_TIMESTAMP=$TODAY_7AM_TIMESTAMP
fi
# 4. Write the timestamp to wakealarm
echo "$WAKEUP_TIMESTAMP" > /sys/class/rtc/rtc0/wakealarm
# 5. Verify the alarm is set (optional)
cat /sys/class/rtc/rtc0/wakealarm
Bash script to set RTC wake-up for 07:00 AM daily
wakealarm
file expects a Unix timestamp in UTC. Ensure your system's RTC is synchronized with UTC for accurate wake-up times. Discrepancies between local time and UTC can lead to incorrect wake-up schedules.1. Create a Wake-up Script
Create a shell script (e.g., /usr/local/bin/set_daily_wakeup.sh
) containing the logic to calculate and set the wakealarm
.
2. Make Script Executable
Grant execute permissions to the script using chmod +x /usr/local/bin/set_daily_wakeup.sh
.
3. Automate Script Execution
To ensure the alarm is set after every boot, add a call to this script in a system startup service (e.g., a systemd service) or a cron job that runs at boot. For a systemd service, you might create /etc/systemd/system/rtc-wakeup.service
.
4. Enable and Start the Service
Enable the systemd service with sudo systemctl enable rtc-wakeup.service
and start it with sudo systemctl start rtc-wakeup.service
.
[Unit]
Description=Set Daily RTC Wake-up Alarm
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/set_daily_wakeup.sh
[Install]
WantedBy=multi-user.target
Example systemd service file for automating RTC wake-up