Hi Jesper/all,

On Sun, 13 Aug 2017 14:13:42 +0200 Jesper Wallin wrote:
> 
> ... someone on IRC also suggested sensorsd or even ksh and a
> cronjob. I personally find it a bit too ducttapey though, especially
> for a feature one would expect on a laptop.


For what its worth, below is an unpriv duck tape cron ksh script,
which I've been meaning to port & package up... (It is very reliable.)

If the battery status is critical, it syslogs & wall(1)s.

When disaster seems iminent, it starts a delayed shutdown(8),
which it later kills if power is plugged.


<operator@fir:~ 0>$ crontab -l | fgrep batt-crit
*/5     *       *       *       *       ~/bin/batt-crit


<operator@fir:~ 0>$ cat ~/bin/batt-crit
#!/bin/ksh
#
#       $Id: batt-crit,v 1.14 2016/05/13 13:22:58 craig Exp $
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#
# Copyright (c) 2015-2016 Craig R. Skinner <skin...@britvault.co.uk>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#


# A/C charger ! disconnected
[[ $(apm -a) -ne 0 ]] && exit


# Battery status ! critical
[[ $(apm -b) -eq 2 ]] || exit 0


alias logger='logger -p daemon.crit'
apm | tr -s '\n' ' ' | logger


batt_lvl=$(apm -l)
[[ ${batt_lvl} -gt 15 ]] && exit
apm | wall
[[ ${batt_lvl} -gt 5 ]] && exit


warning='battery charge level critically low'
shutdown -hp +${batt_lvl} ${warning}
print "shutdown -hp +${batt_lvl} ${warning}" | logger


renice -n 20 $$ > /dev/null

count=100
until [[ ${count} -eq 0 ]]
do
        sleep 20
        [[ $(apm -a) -eq 0 ]] ||
        {
                pgrep -l -f shutdown &&
                {
                        print 'killing...'
                        pkill -l shutdown
                        break
                }
        }
        let count--
done


Reply via email to