You are on page 1of 4

Software & security

Computer games

Life topics

Hillbilly physics

Greatest sites

3D art

Model planes

Wake your Linux up from sleep for a cron job


This article was originally posted on osnews.com on Dec 8, 2010.
Advertise!

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.

Would you like to advertise your product/site on Dedoimedo? Read more

Donate to Dedoimedo! Do you want to help me take early retirement? How about donating some dinero to Dedoimedo? Read more

APIs BioSimilars India

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.

Contract Manufacturing cGMP APIs, Intermediates & BioSimilars


www.InventyS.IN

Cat Rental Power Solution


Diesel & Natural Gas Generators 24/7 Support For Event Or Emergency
www.catelectricpowerinfo.com

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.

Ofertas hasta un -70%


Descuentos Increbles. Ofertas en Tu Ciudad hasta un 70% menos Aqui!
www.GROUPON.com.ar/Ofertas

5 Ways to Help Baby Sleep

Tear-free tips to help your baby sleep through the night


www.SleepThroughNight.com

Proyectos de Agua
Coca-Cola y la Fund. Vida Silvestre te Invitan a Conocer la Iniciativa!
www.CocaCola.com.ar

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.

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. 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.

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 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. Cheers.

FeedBurner

del.icio.us

stumble

digg

reddit

slashdot

Donormyl 25mg Strong Sleeping Pill for Insomnia Fast, Free Worldwide Shipping www.ariz onapharmaceuticals.com Clock Spy Cameras Spy Clock Cameras, Smart Deal. Tiny HD Camera, Free Shipping. www.focalprice.com Parkinson's disease Learn how one can react to the daily challenges of living with PD www.parkinsons-voices.eu

Top

Home

Terms of use

Contact me

About

Copyright @ Dedoimedo.com 2006-2011; all rights reserved

You might also like