A little more detail...
I modified /etc/acpi/power.sh to call the following script right after the includes.
/etc/acpi/battery.pl
Code:
#!/usr/bin/perl
warn "On Battery [$ARGV[0]\n";
my $status = `/usr/bin/acpi -Ba`;
#AC Adapter 1: on-line
my $stat_file = '/var/run/battery_status';
if ($status =~m/on-line/i){
warn "On AC\n";
unlink ($stat_file) if (-e $stat_file);
}else{
warn "On Battery\n";
open (FH, ">$stat_file") or die "Couldn't open battery status file [$stat_file] : $!";
print FH time(),"\n";
}
Here is the cronjob... /usr/local/bin/checkpower
Code:
#!/usr/bin/perl
use warnings;
$ENV{PATH} = '/sbin:/usr/sbin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games';
my $SHUTDOWN_SECS = 60 * 5; # How long to wait before shutdown
my $file = '/var/run/battery_status';
my $batt_time;
if (-e $file){
open (FH, "<$file") or die "Couldn't read file: $!";
$batt_time = <FH>;
chomp($batt_time);
print "On battery for ",time()-$batt_time," seconds\n";
if (time() - $batt_time > $SHUTDOWN_SECS){
warn "Shutting down...";
unlink($file); # Clean up
warn "Hibernating\n";
system('/usr/local/sbin/hibernate --force');
}else{
print $SHUTDOWN_SECS - (time() - $batt_time), " seconds until shutdown\n";
}
}else{
# "Nothing to do";
}
Bookmarks