Wednesday, May 26, 2010

Scheduled Wakeup in Ubuntu

Well, this is hardly related to Tivoli, but just thought of sharing this cool stuff. I have a 5-year old desktop running Ubuntu Lucid used mainly for running scheduled jobs off of cron. So, basically it plays some music everyday for couple of hours and sits idle for the rest of the day. Ideally, I wanted to put the system on standby all the time and waking it up only when the scheduled job needs run. It is relatively easy to do it in Ubuntu (especially if your BIOS supports it).

Here are the exact steps needed on my Lucid Lynx. Please note that you need to have Kernel 2.6.22 or later for this to work.

1) Install the Power management interface tools.
sudo apt-get install powermanagement-interface
2) Copy the following code somewhere in your filesystem and save it as "suspend_x_hours.sh".
#!/bin/bash
# This script puts the system under standby mode for x hours
usage() {
echo "usage: $0 <n-hours>"
echo "where <n-hours> is the number of hours to be on standby"
exit 0

}
if [ $# -ne 1 ]
then
usage
fi

PATH=$PATH:/usr/sbin
hours=$1
echo 0 > /sys/class/rtc/rtc0/wakealarm
echo `date '+%s' -d "+ $hours hours"` > /sys/class/rtc/rtc0/wakealarm
pmi action suspend

3) Schedule the script in root's crontab. e.g the following crontab entry runs at 8PM and puts the system in sleep for 10 hours, waking it up at 6:00 AM.
00 20 * * * /home/venkat/bin/suspend_x_hours.sh 10 2>/dev/null

That's it. It takes only about 10 seconds to resume from sleep and it even restores your SSH sessions when it comes back from sleep!
Hope you find it useful.

8 comments:

Unknown said...

I'd like to use this but haven't been able to get to work yet. If I copy the code exactly and save it to "suspend_x_hours.sh" in my /etc directory, then, as I understand it, I should enter the following script in root's crontab:

00 20 ***/etc/suspend_x_hours.sh 10 2>dev/null

This isn't working for me with 10.04 Kernel 2.6.32-28-server.

Should I change any of the values in the "suspend_x_hours.sh" code, such as replacing "n" in lines 4 & 5 and "hours" in line 17 with my chosen values?

I'd like to use this but I don't know what I'm doing wrong.

Unknown said...

Does it matter that I don't have a "wakealarm" file in /sys/class/rtc/rtco/ ? I see I have a text document file called "wakealarm" in /sys/dev/char/254:0

Venkat said...

Mark,

If you don't have wakealarm in /sys/class/rtc/rtc0 (zero not oh), it probably indicates either your hardware is not ACPI supported/enabled or due to a kernel bug.

https://bugs.launchpad.net/ubuntu/+source/linux/+bug/195263

Hope this helps,

Unknown said...

Hi Vencat,

Thanks for your reply. After reinstalling 10.04 I've now found the wakealarm in /sys/class/rtc/rtc0. I checked ACPI is enabled in the bios, and it is, and I'm wondering why I can't get it to work.

To put the PC in standby between 10pm and 7am, should I do this?

(1) Copy and paste the code exactly as you've written it and save it as suspend_x_hours.sh in /home/mark/.

(2) Add Crontab entry: 00 22 * * * /home/mark/suspend_x_hours.sh 9 2>/dev/null

Or must I change something in code in (1) to specify the sleep and wake-up times? Maybe I'm misunderstanding something.

Venkat said...

Mark,

There is nothing to change inside the code. All you need to do is copy the code and put in the root's crontab just like you have done.

As you can see, the script is a simple one. It takes the number of hours as parameter, calculates the wake up time, puts the wake up time in wakealarm file and calls the suspend action.

Check to see couple of things.

1) Run "sudo pmi action suspend" and see if it correctly suspends the system.

2) See if the wakealarm file contains the right wake up time by running the following command.

perl -e "print scalar(localtime(`cat /sys/class/rtc/rtc0/wakealarm `))"

Hope this helps,

Unknown said...

Venkat,

Running "sudo pmi action suspend" does put the PC to sleep and the case fan stops spinning.

When the crontab entry runs, the PC appears to sleep. There is no output to the screen but the case fan still runs and DVD draw opens and closes so I'm not sure what state it is actually in.

Could it be that the script isn't working as it should do in my system?

Anonymous said...

Thanks for posting this. I found it very useful when trying to get my server to sleep at night.

I had trouble with the "pmi action suspend" command. For some reason, my system would hang when trying to wake up from this command. I found that by replacing it with:

dbus-send --system --print-reply --dest="org.freedesktop.UPower" \
/org/freedesktop/UPower org.freedesktop.UPower.Suspend

I was able to suspend the system and have it wake up successfully with wakealarm. Maybe this tip will be useful to someone else who happens upon this post.

Anonymous said...

Thanks for the tip. It will surely come in handy.

-Venkat