2017年6月1日 星期四

Linux Wake From Sleep Automatically

Want to put your Linux PC into sleep or hibernate mode and have it automatically wake at a specific time? You can easily do this with the rtcwake command, included by default with most Linux systems.
This can be useful if you want your computer to do something at a specific time, but don’t want it running 24/7. For example, you could put your computer to sleep at night and have it wake up before you do to perform some downloads.

Using rtcwake

The rtcwake command requires root permissions, so it must be run with sudo on Ubuntu and other Ubuntu-derived distributions. On Linux distributions that don’t use sudo, you’ll have to log in as root with the su command first.
Here’s the basic syntax of the command:
sudo rtcwake -m [type of suspend] -s [number of seconds]
For example, the following command suspends your system to disk (hibernates it) and wakes it up 60 seconds later:
sudo rtcwake -m disk -s 60

Types of Suspend

The -m switch accepts the following types of suspend:
  • standby – Standby offers little power savings, but restoring to a running system is very quick. This is the default mode if you omit the -m switch.
  • mem – Suspend to RAM. This offers significant power savings – everything is put into a low-power state, except your RAM. The contents of your memory are preserved.
  • disk – Suspend to disk. The contents of your memory are written to disk and your computer is powered off. The computer will turn on and its state will be restored when the timer completes.
  • off – Turn the computer off completely. rtcwake’s man page notes that restoring from “off” isn’t officially supported by the ACPI specification, but this works with many computers anyway.
  • no – Don’t suspend the computer immediately, just set the wakeup time. For example, you could tell your computer to wake up at 6am. After that, can put it to sleep manually at 11pm or 1am – either way, it will wake up at 6am.

Seconds vs. Specific Time

The -s option takes a number of seconds in the future. For example, -s 60 wakes your computer up in 60 seconds, while -s 3600 wakes your computer up in an hour.
The -t option allows you to wake your computer up at a specific time. This switch wants a number of seconds since the Unix epoch (00:00:00 UTC on January 1, 1970). To easily provide the correct number of seconds, combine the date command with the rtcwake command.
The -l switch tells rtcwake that the hardware clock is set to local time, while the -u switch tells rtcwake that the hardware clock (in your computer’s BIOS) is set to UTC time. Linux distributions often set your hardware clock to UTC time and translate that to your local time.
For example, to have your computer wake up at 6:30am tomorrow but not suspend immediately (assuming your hardware clock is set to local time), run the following command:
sudo rtcwake -m no -l -t $(date +%s -d ‘tomorrow 06:30’)


More Tips

Use the && operator to run a specific command after rtcwake wakes your system from sleep. For example, the following command suspends your computer to RAM, wakes it two minutes later, and then launches Firefox:
rtcwake -m mem -s 120 && firefox
Integrate the rtcwake command into a cron script to automatically wake your computer at a specific time. The -m no switch can also be useful in a cron script. For example, you could run the rtcwake -m no -s 28800 command in a cron script at 10pm every day. This would set your computer to wake up in 28800 seconds at 6:00am. However, your computer wouldn’t go to sleep immediately – you could put it to sleep at 11pm or 1am and it would still wake at 6am normally.

Caveats

  • RTC stands for real-time clock. rtcwake uses your computer’s hardware clock, which you can set in your BIOS, to determine when your computer will wake up. If you’re using an old computer with a dying CMOS battery that can’t keep the clock running properly, this won’t work.
  • If sleep, suspend to RAM, or hibernate don’t work properly with your Linux system – perhaps because Linux doesn’t have the drivers to make them work properly with your hardware – this may not work.
  • Be careful when setting a laptop to automatically wake at a specific time. You wouldn’t want it waking up, running, and overheating or running down its battery in a laptop bag.

沒有留言:

張貼留言