4c59cfb584149dd7ac6de676bed7f4849f646457
   1#! /bin/bash
   2
   3# Send warning email and shut down system when UPS battery is low.
   4# Andrew Lorimer, January 2020
   5
   6recipient="andrew"
   7
   8ctime=`date`
   9state=`pwrstat -status`
  10model=`echo $state | grep -oP 'Model Name\.+ \K\w+'`
  11runtime=`echo $state | grep -oP 'Remaining Runtime\.+ \K\d+\s?\w+(?=\.)?'`
  12level=`echo $state | grep -oP 'Battery Capacity\.+ \K\d+'`"%"
  13
  14echo -e "Battery level is $level (expected runtime $runtime) as of $ctime. Shutting down system in one minute.\n$state" | mail -a "Importance: High" -a "Content-Type: text/plain; charset=UTF-8" -s "⚠️🔋 Battery level $level on $model" $recipient
  15sleep 60
  16
  17echo "Starting system shutdown due to low battery" | systemd-cat -t `basename "$0"`
  18shutdown now