Wake Up

| | Comments (0)
I don't know if there exists something better, but I couldn't find it, and I was curious anyway, and so I wrote my own.

I need to wake my computer at 3 a.m. to run nightly backups (using Backup.app from mac.com), but if the computer is asleep, it doesn't work. IOPMSchedulePowerEvent() can wake the computer at a given time, but I had no existing Perl interface to it. I could have added it to a Mac:: module, but in this case, I didn't have the time, and I currently have no use for it other than this one script. Therefore, Inline.

One especially interesting thing about the code -- to me -- is that it uses the CFAbsoluteTime data type, which -- for reasons unknown to me -- is in seconds since January 1, 2001, GMT. So the C code keeps a constant to calculate the difference from perl's time().

# i use this to make sure my computer wakes up by 3 a.m. every morning,
# so my backups can run
# i run it from cron every 15 minutes (the computer is set to sleep
# after 15 minutes of inactivity)
# */15 * * * * /Users/pudge/bin/wake.plx
# pudge@pobox.com 2004-04-01
use strict;
use warnings;
use Time::Local;
use Inline  C => 'DATA',
    LDDLFLAGS => '-bundle -flat_namespace -undefined suppress -framework Carbon',
    INC       => '-I/Developer/Headers/FlatCarbon';
my $now = time();
my @today = localtime($now);
@today[0, 1, 2] = (0, 55, 2);
my $then = timelocal(@today);
$then += 86400 if $now > $then;
schedule_event($then, 'wake');
#undef Move
#undef I_POLL
#undef DEBUG
#include <Carbon.h>
#include <mach/error.h>
// "type" defines here
// can be sleep, wake, shutdown, poweron, and wakepoweron
//#include <IOKit/pwr_mgt/IOPMKeys.h>
// difference between Unix epoch and CFAbsoluteTime epoch
#define EPOCH_DIFF 978307200
long schedule_event(long seconds_time_to_wake, const char * event_name)
    CFDateRef time_to_wake;
    CFStringRef type;
    IOReturn io_error;
    seconds_time_to_wake -= EPOCH_DIFF;
    time_to_wake = CFDateCreate(kCFAllocatorDefault, (CFAbsoluteTime)seconds_time_to_wake);
    type = CFStringCreateWithCString(kCFAllocatorDefault, event_name, NULL);
    io_error = IOPMSchedulePowerEvent(time_to_wake, NULL, type);
    if (io_error != 0) {
        printf("system: 0x%x, subsystem: 0x%x, code: 0x%x\n",
    return io_error;


Leave a comment

<pudge/*> (pronounced "PudgeGlob") is thousands of posts over many years by Pudge.

"It is the common fate of the indolent to see their rights become a prey to the active. The condition upon which God hath given liberty to man is eternal vigilance; which condition if he break, servitude is at once the consequence of his crime and the punishment of his guilt."

About this Entry

This page contains a single entry by pudge published on April 3, 2004 5:17 PM.

#66666 was the previous entry in this site.

Sunday Thoughts is the next entry in this site.

Find recent content on the main index or look in the archives to find all content.