New script to suspend on almost dead battery

I dug up this gem I wrote way back after my lappy completely shut down when the battery ran out w/o warning to prevent this from happening again, after modifying to use ACPI:

#!/usr/bin/perl -w

#JWT:  THIS SCRIPT PERIODICALLY CHECKS THE BATTERY STATUS AND DISPLAYS A
#BIG IMAGE OF AN EMPTY BATTERY WARNING TO THE X SCREEN IF BATTERY IS ABOUT
#TO RUN OUT.  IT IS STARTED BY asstartup.sh IN THE ~/…/AfterStep DIRECTORY
#AND IS STOPPED AND RESTARTED BY /etc/apm/event.d/30local

my $warnPct = ‘5’;
my $suspPct = ‘2’;
my $pollEverySec = ’90’;
my $imageViewer = ‘/usr/local/bin/xv’;
my $batImage = ‘/usr/share/pixmaps/warnlowbat.xpm’;
my $runlevel = `/sbin/runlevel`;
my $s2dCmd = ‘/usr/local/bin/s2disk’;
my $alreadyWarned = 0;

$SIG{ALRM} = sub {
    #$_ = `/usr/bin/apm`;         #APM
    $_ = `/usr/bin/acpi -b`;  #  if (!$_ || /No APM/);   #ACPI
    unless ($_ && length($_) > 0)
    {
        die "-no power data ($@), exiting!";
    }
    chomp;
    $batpct = $1  if (/(\d+)\%/o);
    if (/Discharging/io)
    {
        if ($batpct < $suspPct)
        {
            system $s2dCmd;
            $alreadyWarned = 0;
        }
        elsif ($batpct < $warnPct && !$alreadyWarned)
        {
            system "$imageViewer $batImage &"  if ($runlevel =~ /5/o);
            system ‘/usr/bin/wall’, "Battery is VERY LOW ($batpct), will suspend @ $suspPct% – Plug in A/C NOW!";
            $alreadyWarned = 1;
        }
    }
    alarm($pollEverySec);
};

alarm(5);
while (1)
{
    sleep(1000);
}

__END__

Advertisements

Feel Free to Comment (Name/Email/Website optional):

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: