Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Fletch (75)

Fletch
  (email not shown publicly)
http://phydeaux.org/
AOL IM: lemurnomicon (Add Buddy, Send Message)

Frink? Frink? Phtang!

Journal of Fletch (75)

Monday September 08, 2003
10:16 AM

nosleep 'till Brooklyn

[ #14582 ]

Or at least until that long running wget completes. This will temporarily set the OS X energy saver sleep timer to `never', run the specified command, and then restore the sleep time.

#!/usr/bin/perl
##
## nosleep -- Disable sleep while command runs (OS X)
##
use warnings;
use strict;

my @pm_set_cmd = qw( pmset sleep );

unshift @pm_set_cmd, 'sudo' unless $> == 0; ## Setting needs root privs

## Figure out what sleep's set to
my $old_sleep = undef;

open( GET, "pmset -g live |" ) or die "Can't open pipe from pmset -g: $!\n";

while( <GET> ) {
  if( /sleep\s+(\d+)/ ) {
    $old_sleep = $1;
    last;
  }
}

close( GET );

die "Couldn't determine current sleep setting.\n" unless defined $old_sleep;

my $ret;

## Set sleep to 0 (never)
$ret = system( @pm_set_cmd, 0 );
die "Problem setting sleep: @{[ $? >> 8 ]}\n" unless $ret == 0;

## Run command passed as argument
$ret = system( @ARGV );
warn "Problem running command: @{[ $? >> 8 ]}\n" unless $ret == 0;

## Revert to old sleep value
$ret = system( @pm_set_cmd, $old_sleep );
die "Problem setting sleep: @{[ $? >> 8 ]}\n" unless $ret == 0;

exit 0;

__END__
You may wish to add something along these lines to your /etc/sudoers:

Cmnd_Alias      PMSET = /usr/bin/pmset sleep [0-9]*
%admin  ALL=(ALL) ALL, NOPASSWD: PMSET

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.