Here’s the latest in our new series on OS tips from power users: 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.
Requirements
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, 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 (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
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.
Caveats
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.
Usage model
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.
Pay attention to 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.
Other things
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.
Conclusion
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 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.
Cheers.
About the author:
Igor Ljubuncic aka Dedoimedo is the guy behind dedoimedo.com. He makes a living out of his
very hobby – Linux, and holds a bunch of certifications that make a nice pile in the bottom
drawer.
Call for submissions:
Are you a power user? Can you roll up your sleeves and give other users of your platforms some in-depth advice on getting the most out of their machines? Then submit it to OSAlert!
nice tip. Keep these type of articles coming OSAlert. Bookmarked for future reference.
cheers
after all these years, OSAlert is going to post articles about operating systems, no way
Edited 2010-12-08 19:57 UTC
…Thank you!
Hi!
I made a simple bash script to tell my computer to wake at a certain time without much fuss about the time format. It also corrects for a time which seems in the past but actually is in the future.
Here are some use case.
It’s 13:00 (01:00 PM)
wake_at 15 #Will wake at 15:00
wake_at 16:45 #Will wake at 16:45
wake_at 8 #Will wake the next morning at 8
This means that when you go to sleep, you can go to a console (or anywhere you can type commands) and simply type the time you want your computer to wake at. Sure, you can’t tell it to wake in a week with it, but it’s not its intended use; it has a limited scope of 24 hours.
Here’s the script:
————
#!/bin/bash
wake_at=`date –date “$1” +%s`
now=`date +%s`
#Add a full day to the time if it is lesser than now…
#This allows calling `wake_at 8:00` the evening before.
if [ $wake_at -lt $now ]; then
let wake_at=$wake_at+86400
fi
#Then execs rtcwake
exec rtcwake -l -t $wake_at -m no
————
Hope it helps!
Thanks for extra info, really helped. Hopefully this will become standard feature in OS itself(well distro atleast) with easy UI. In future corporations might want this kind a feature to low down energy use. Having work machines start and shutdown automaticly in certain times would save tons of energy. It’s so common to see people just leave PCs open for whole night, doing nothing.
Thanx!
I’ll try to use this in my setup. Right now I have an old laptop which on resume will launch an mp3 internet radio. Since it resume from suspend on the press of a key, as soon as the real alarm clock rings, I press a key and I have my real radio.
I think I did not investigated an automatic way of doing it for two reason:
1) Ease changing the wakeup time. I did not know how to do it. Now I do
2) I think I have an issue with the hardware clock. I changed the bios battery but I’m not sure it fixed it. I’ll check it back because of your article
What if it’s a laptop that is configured to suspend when lid is closed – and the lid is closed at the alarm time? Wakeup + suspend immediately?
What if it’s a laptop that is configured to suspend when lid is closed – and the lid is closed at the alarm time?
When the lid is closed it emits a signal triggering the suspend. But if the lid is already closed when the machine is powered on it won’t emit a signal. The system could of course poll for the state of the lid but as far as I know no sane Linux distro handles it that way.
Thus I’d say the system wakes up at specified time and does what it was supposed to, and keeps on running unless you added another suspend command at the end of the jobs.
OT: What the hell is that picture, someone’s bunghole?
Weird…
It’s a closed eye, though not a very good picture of one.
Why not combine the four cron jobs into one? Something like:
rdate -s server; hwclock –systohc; rtcwake args; overNightScript.sh
The overNightScript.sh script should start as soon as the system wakes up from rtcwake.
Or course, since this must run as root, full paths should be used; in addition, an additional rtcwake call will probably be needed to put the machine back to sleep until morning after overNightScript.sh completes.
This avoids any dependency on implicit timing, and keeps everything together in a logical unit.
Blurred on purpose for artistic impression