LOGO

Wake From Sleep on Linux: A How-To Guide

August 8, 2012
Wake From Sleep on Linux: A How-To Guide

Automating Wake-Up Times on Linux with rtcwake

Do you need to schedule your Linux PC to enter sleep or hibernation and then automatically resume at a designated time? The rtcwake command provides a straightforward solution, as it’s typically pre-installed on the majority of Linux distributions.

Benefits of Scheduled Wake-Ups

This functionality proves valuable when you require timed tasks without continuous operation. For instance, you might wish to conserve energy by having your system sleep during off-peak hours.

A practical application involves scheduling your computer to wake up prior to your usual start time. This allows it to complete tasks like downloads while you are not actively using it.

How rtcwake Works

The rtcwake utility leverages the Real Time Clock (RTC) to accurately schedule wake-up events. It essentially instructs the RTC to trigger a system wake-up at the specified moment.

This method is more reliable than relying on scheduled tasks that require the system to be fully operational to function. It ensures wake-up even if the system is completely powered down (depending on hardware support).

Example Use Cases

  • Automated backups scheduled for off-peak hours.
  • Pre-downloading files for the next day’s work.
  • Running system updates during periods of inactivity.

By utilizing rtcwake, you can optimize your Linux system’s energy consumption and automate routine tasks efficiently.

Employing rtcwake for System Wake-Up

Utilizing the rtcwake utility necessitates administrative privileges. Consequently, on Ubuntu and its derivatives, execution must be preceded by sudo. For Linux systems lacking sudo, initial login as the root user via the su command is required.

The fundamental command structure is as follows:

sudo rtcwake -m [type of suspend] -s [number of seconds]

As an illustration, the subsequent command initiates system hibernation and schedules a wake-up event after a duration of 60 seconds:

sudo rtcwake -m disk -s 60

The rtcwake tool leverages the Real Time Clock (RTC) to achieve scheduled wake-ups. This method is particularly useful for automating tasks or minimizing energy consumption.

Understanding the Parameters

The -m flag specifies the desired suspend mode. Common options include:

  • mem: Suspends to RAM (sleep mode).
  • disk: Hibernates to disk.
  • standby: Enters a low-power standby state.

The -s flag defines the wake-up delay in seconds. This value determines how long the system remains suspended before being reactivated.

Proper configuration of the RTC is essential for rtcwake to function correctly. Ensure the system clock is synchronized for accurate timing.

Suspend Modes Explained

The -m option allows for the selection of various suspend states.

Available Suspend Types

Several distinct suspend modes are supported, each offering a different balance between power conservation and resumption speed.

  • standby – This mode provides minimal power reduction, but allows for a very rapid return to full operation. It functions as the default suspend type when no specific mode is designated using the -m switch.
  • mem – Utilizing suspend to RAM, this method achieves substantial power savings. All system components, excluding RAM, are transitioned to a low-power condition, ensuring data preservation within the memory.
  • disk – In this suspend-to-disk mode, the entire contents of the system's memory are saved to the hard drive before the computer is fully powered down. Upon timer completion, the system restarts and restores its previous state.
  • off – This option initiates a complete system shutdown. While the ACPI specification doesn't formally endorse resuming from a complete "off" state, it often functions correctly on numerous computer systems.
  • no – This setting prevents immediate system suspension, instead focusing on establishing a designated wake-up time. For instance, a computer can be scheduled to activate at 6:00 AM.

Following this scheduling, manual suspension can be initiated at times like 11:00 PM or 1:00 AM, with the system consistently waking at the pre-defined 6:00 AM time.

Waking Your System: Time Intervals vs. Specific Times

Utilizing the -s flag, you can specify a wake-up time as a number of seconds from the present. For instance, employing -s 60 will initiate a wake-up of your computer in 60 seconds. Similarly, -s 3600 will schedule a wake-up after one hour.

Alternatively, the -t option enables you to set a precise wake-up time. This parameter requires a value representing the number of seconds elapsed since the Unix epoch – January 1, 1970, 00:00:00 UTC. To conveniently determine the appropriate number of seconds, integrate the date command alongside the rtcwake command.

The -l flag instructs rtcwake that the hardware clock is configured to local time. Conversely, the -u flag indicates that the hardware clock, located within your computer’s BIOS, is set to UTC time. Many Linux distributions default to setting the hardware clock to UTC, subsequently converting it to your local time zone.

To illustrate, if you wish to have your computer awaken at 6:30 AM tomorrow without immediately suspending, assuming your hardware clock is set to local time, execute this command:

sudo rtcwake -m no -l -t $(date +%s -d ‘tomorrow 06:30’)

This command leverages the date utility to calculate the seconds since the epoch for the specified time and date.

Understanding the Components

  • sudo: Executes the command with administrator privileges.
  • rtcwake: The command-line utility for waking the system.
  • -m no: Prevents the system from suspending immediately after waking.
  • -l: Specifies that the hardware clock uses local time.
  • -t: Indicates a specific wake-up time in seconds since the epoch.
  • date +%s -d ‘tomorrow 06:30’: Calculates the number of seconds until tomorrow at 6:30 AM.

Proper configuration of the time zone and hardware clock settings is crucial for accurate wake-up times.

Additional Guidance for rtcwake Utilization

The logical AND operator, represented by &&, allows for the execution of a designated command following the system's awakening from sleep initiated by rtcwake. This enables automated task initiation post-wake-up.

Consider the following example: your computer is suspended to RAM, scheduled to resume after a two-minute interval, and subsequently launches the Firefox web browser.

rtcwake -m mem -s 120 && firefox

Automating Wake-Ups with Cron

To schedule automatic system wake-ups, incorporate the rtcwake command into a cron script. This facilitates hands-free operation at predetermined times.

The -m no option proves particularly beneficial when used within a cron environment. It ensures the system doesn't attempt to enter a sleep state again immediately after waking.

For instance, executing rtcwake -m no -s 28800 via a cron script at 10:00 PM daily will configure the computer to awaken after 28800 seconds – precisely at 6:00 AM.

It's important to note that the computer will not enter sleep mode instantaneously upon command execution. You can independently initiate sleep at a later time, such as 11:00 PM or 1:00 AM, and the 6:00 AM wake-up will still function as intended.

Important Considerations

RTC, which signifies real-time clock, is a crucial component in the functionality of the rtcwake utility. This tool leverages your computer’s hardware clock – typically configured within the BIOS settings – to schedule system wake-ups.

CMOS Battery Dependency

Successful operation is contingent upon a functioning CMOS battery. If your computer is older and the CMOS battery is failing, potentially leading to inaccurate timekeeping, the rtcwake feature may not perform as expected.

System Sleep and Suspend Compatibility

The effectiveness of rtcwake is also tied to the proper functioning of system sleep states. If features like sleep, suspend to RAM, or hibernation are unstable or unsupported on your Linux system due to driver issues, rtcwake’s reliability may be compromised.

Laptop Wake-Up Precautions

When configuring automatic wake-up times on a laptop, exercise caution. Unintended wake-ups can lead to overheating or excessive battery drain if the laptop is enclosed in a bag or other confined space.

It’s important to be mindful of these potential issues to ensure a smooth and reliable experience with the rtcwake utility.

#Linux#wake from sleep#wake on lan#WOL#power management#auto wake