Wake Your Linux Up From Sleep for a Cron Job

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,


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.


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.


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.


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

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 OSNews!


  1. Calipso 2010-12-08 7:27 pm EST
  2. syphon 2010-12-08 7:55 pm EST
  3. Tuishimi 2010-12-08 8:07 pm EST
  4. samueldr 2010-12-08 8:12 pm EST
    • Karitku 2010-12-09 9:21 am EST
  5. big_gie 2010-12-09 2:32 am EST
  6. vivainio 2010-12-09 9:19 am EST
    • WereCatf 2010-12-09 9:36 am EST
  7. Bobthearch 2010-12-09 6:47 pm EST
    • David Adams 2010-12-09 11:19 pm EST
  8. james_parker 2010-12-09 8:16 pm EST
  9. Dedoimedo 2010-12-10 10:21 am EST