Updated: December 8, 2010
Here's a seemingly trivial task. You have a computer, most likely a laptop, that you wish to keep suspended while you're not working. For example, let's say overnight. At the same time, you wish to run a handful of maintenance tasks, like backups and cleanup, which you don't normally do during the day. So you need a mechanism that will send your machine to sleep, wake it up when necessary, run cron jobs, then send it back to sleep again.
Possible? Well, if it weren't, I would not be writing this tutorial, but it definitely is not trivial. In this tutorial, I will show you how you can do this, in a relatively simple and safe manner. We will check the requirements for the task. We will examine a typical use case. We will discuss the many small-print items revolving around this idea, e.g. why you need to be extra careful with waking from sleep for cron jobs. And we'll do a few other things, besides.
To make your computer sleep, you will need a modern kernel that supports ACPI and sleep modes. In other words, any modern Linux will do. You will be able to standby (S1) or suspend to RAM (S3) your Linux system without any major fuss. However, for more complex sleep modes, you will need a modern BIOS.
You will also need to have root or sudo permissions, because you won't be able to send your operating to sleep or wake it up otherwise.
Tool of the trade: rtcwake
rtcwake is a built-in kernel utility that allows making a machine sleep and wake, using a RTC (Real Time Clock) framework driver that supports standard driver model wakeup flags. rtcwake is a command line utility, so can easily script your usage models, including cron, a.k.a scheduled jobs. To know a little more about the utility, you may want to read the man page.
Using rtcwake is fairly simple. For example, to just dispatch your machine to sleep right now and wake it up in a few moments:
rtcwake -m mem -s 180
What do we have here?
-m flag specifies the mode - standby, suspend to RAM, suspend to disk (hibernate), and powered on. -s flag tells us how much time we want our machine to sleep (in seconds).
This is a trivial example that will sleep my machine for three minutes and then wake it up. Tested and works very reliably. I could have taken a video of my box coming back to life, but I hope you trust my word.
Here's an innocent screenshot showing the wakeup message:
Alternatively, you can use the -t flag to specify the exact time when to wake your machine.
Now, why using rtcwake can be tricky?
There are many reasons why casual users should be careful about using this tool. First, it requires root permissions. Second, misuse could accidentally cause loss of data. Third, if your hardware does not support sleep modes well enough or one of your drivers does not like being disturbed, you could end with application or kernel crashes.
Now, to use rtcwake properly, you need to have your BIOS time set properly. If you have an old box and the BIOS battery is near its end, there could be a time difference between your system time and your hardware clock, which could lead to machine being woken or put to sleep at wrong times.
You must also make sure that if you wake your machine, especially if it's a laptop, that it does not end up boiling an egg inside a bag, with no ventilation, or being left to work on battery power for too long, causing it to hibernate or shutdown after running out of juice.
If you're confident about all these, rtcwake is good for you.
Now, how would I go about using rtcwake?
If I wanted my machine to sleep overnight, wake for a short maintenance, go to sleep again and wake early in the morning, I would have four crons running. The first, to synchronize the OS clock with UTC (via ntp). The second, to synchronize hardware clock with UTC. The third, to put the machine to sleep every evening, using rtcwake and specify the exact moment when it should wake up later. The fourth cron would run a few minutes after the machine wakes and do whatever needs to be done. After all the tasks are complete, it would sleep the machine once more and make it wake up in the morning.
Make sure you leave a few minutes gap between waking up and running cron jobs, to avoid any delicate out-of-sync problems, especially if you know your BIOS is flaky.
Lastly, you can tweak your machine sleep and wake using BIOS, if it supports those. Likewise, you can work with the acpitool to query and set your ACPI values. acpitool can also allow the machine to be put into standby, toggle LCD brightness level, set fan speeds, etc. Lastly, on older kernels, instead of rtcwake, you will want to use the apmsleep tool.
There you go. Now, if you want, you can have your laptops self-attend to their needs, without you pushing the on/off button all the time or being there when you want to sleep the machine. Using the command-line and scripts allows you to fully administer your box and save power without any human intervention.
Do note that the software-to-hardware interface is always a bit tricky and there can be issues with hardware and your drivers. If you have bad RAM, an old BIOS with a weak battery, an unstable machine, or known issues with sleep states, causing some of your system resources to not function properly, then you should by all means avoid using rtcwake or any other similar tool to prevent possible data loss and crashes. Don't forget to do your math properly, so you don't overdo your system resources if running on battery or miss the scheduled tasks altogether.
That would be all.