/usr/local/bin/checkpower

The second half of getting this to work is the following script which is called from cron every 3 minutes. (Change to suite your tastes.)

So if we are on battery more than $SHUTDOWN_SECS, the next time [b]checkpower[/b] is run, it will issue a hibernate command.

#!/usr/bin/perl
use warnings;

$ENV{PATH} = '/sbin:/usr/sbin:/usr/local/bin:/usr/bin:/bin';

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 = ;
   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";
}
Scroll to top