Advertisement
Guest User

battery_warning

a guest
Feb 14th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.80 KB | None | 0 0
  1. #!/bin/bash
  2. ########################################################################
  3. #    Name: battery_warning, Author: Henning Hollermann,                #
  4. #    laclaro@mail.com, http://laclaro.wordpress.com                    #
  5. #                                                                      #
  6. #    This program is free software: you can redistribute it and/or     #
  7. #    modify it under the terms of the GNU General Public License       #
  8. #    as published by the Free Software Foundation, either version 3    #
  9. #    of the License, or (at your option) any later version             #
  10. #                                                                      #
  11. #    This program is distributed in the hope that it will be useful,   #
  12. #    but WITHOUT ANY WARRANTY; without even the implied warranty of    #
  13. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      #
  14. #    GNU General Public License for more details.                      #
  15. #                                                                      #
  16. #    You should have received a copy of the GNU General Public License #
  17. #    along with this program. If not, see <http://gnu.org/licenses/>.  #
  18. #                                                                      #
  19. #    Bash script to be run in background to notify the user(s) on      #
  20. #    critical battery level and to shut down the system if the charge  #
  21. #    level gets too low                                                #
  22. #                                                                      #
  23. #    To send broadcast-messages to all users while running as root     #
  24. #    it uses the notify-send-broadcast script. This is available       #
  25. #    according to the terms of the GPL as well                         #
  26. #                                                                      #
  27. ########################################################################
  28.  
  29. # USER SETTINGS
  30.  
  31. # sleep time for main loop
  32. SLEEP="60"
  33. # low battery in % (warning begins)
  34. LOW="15"
  35. # critical battery in % (execute action)
  36. CRITICAL="13"
  37. # Seconds to delay shutdown after critical battery level is reached
  38. MAX_DELAY="180"
  39. # on this level ACTION is executed
  40. SUPERCRITICAL="10"
  41. # action performed on SUPERCRITICAL
  42. # Note: executing user has to have permissions to do so
  43. ACTION="sudo /sbin/shutdown now -h"
  44. # notification sound command
  45. PLAY="paplay /usr/share/sounds/freedesktop/stereo/dialog-warning.oga"
  46. # ICONS to display
  47. ICON_supercritical=dialog-warning
  48. ICON_batt_low=battery-caution
  49. ICON_batt_plugged=gnome-power-manager
  50.  
  51. # SYSTEM SETTINGS
  52.  
  53. # Battery-Name and AC-Adapter-Name
  54. BAT="BAT0"
  55. AC="AC"
  56. # Files to be found in
  57. AC_ONLINE_FILE="/sys/class/power_supply/$AC/online"
  58. BAT_PRESENT_FILE="/sys/class/power_supply/$BAT/present"
  59. ENERGY_FULL_FILE="/sys/class/power_supply/$BAT/energy_full"
  60. ENERGY_NOW_FILE="/sys/class/power_supply/$BAT/energy_now"
  61. STATE_FILE="/sys/class/power_supply/$BAT/status"
  62. CHARGE_FILE="/sys/class/power_supply/$BAT/energy_now"
  63. # Script to send X-broadcast messages as root
  64. notify_send_broadcast="/usr/local/bin/notify-send-wall"
  65. #notify_send_broadcast="/usr/bin/notify-send"
  66. # file for the process ID
  67. # executing user has to have permissions to write this file
  68. PIDFILE="/var/run/battery_warning/pid"
  69.  
  70. # END OF SETTINGS
  71. #[ ! -d /var/run/battery_warning/ ] && mkdir /var/run/battery_warning/
  72.  
  73. start()
  74. {
  75.     echo "Starting battery-warning...";
  76.     if [ ! -e $PIDFILE ]; then
  77.         if [ -w $PIDFILE ]; then
  78.             echo $$ > $PIDFILE
  79.         else
  80.             echo "Warning: cannot create file $PIDFILE."
  81.         fi
  82.     fi
  83.  
  84.         MAX_BATTERY=$(cat $ENERGY_FULL_FILE)
  85.         LOW_BATTERY=$(($LOW*$MAX_BATTERY/100))
  86.         CRITICAL_BATTERY=$(($CRITICAL*$MAX_BATTERY/100))
  87.         SUPERCRITICAL_BATTERY=$(($SUPERCRITICAL*$MAX_BATTERY/100))
  88.         # Main Loop
  89.         while [ true ]; do
  90.  
  91.             PRESENT=$(cat $BAT_PRESENT_FILE)
  92.             AC_online=$(cat $AC_ONLINE_FILE)
  93.  
  94.             if [ "$PRESENT" = "1" ] && [ "$AC_online" = "0" ] ; then
  95.  
  96.                 STATE=$(cat $STATE_FILE)
  97.                 CAPACITY=$(cat $ENERGY_NOW_FILE)
  98.  
  99.                 if [ "$CAPACITY" -lt "$CRITICAL_BATTERY" ] && \
  100.                    [ "$STATE" = "Discharging" ];
  101.                 then
  102.                     DELAY=0; INTERVAL=10;
  103.                     while [ "$AC_online" = "0" ]; do
  104.                         if [ $CAPACITY -lt $SUPERCRITICAL_BATTERY ] || \
  105.                            [ $DELAY -ge $MAX_DELAY ]; then
  106.                             ${notify_send_broadcast} -u critical \
  107.                                 -i $ICON_supercritical \
  108.                                 -T 15000 -t 'Achtung' \
  109.                                 -m 'Das System wird heruntergefahren!'
  110.                             sleep 3
  111.                             $($ACTION)
  112.                             [ $? -ne 0 ] && ${notify_send_broadcast} -u critical \
  113.                                 -i $ICON_supercritical \
  114.                                 -m 'DAS SYSTEM KONNTE NICHT HERUNTERGEFAHREN
  115. WERDEN. SCHALTEN SIE DEN COMPUTER SOFORT AUS UM DATENVERLUST ZU VERHINDERN!' \
  116. -T 0 -t 'FEHLER'
  117.                         else
  118.                             ${notify_send_broadcast} -u normal \
  119.                                 -i $ICON_batt_low \
  120.                                 -m "Sehr niedriger Ladestand
  121. ($((100*$CAPACITY/$MAX_BATTERY))%). Das System wird in
  122. $(($MAX_DELAY-$DELAY+$INTERVAL)) Sekunden oder bei einem Ladestand
  123. unter $SUPERCRITICAL% automatisch heruntergefahren." \
  124.                                 -T $((1000*$INTERVAL-200)) -t "Achtung"
  125.                         fi
  126.                         sleep $INTERVAL
  127.                         DELAY=$(($DELAY+$INTERVAL))
  128.                         CAPACITY=$(cat $ENERGY_NOW_FILE)
  129.                         AC_online=$(cat $AC_ONLINE_FILE)
  130.                     done
  131.                     ${notify_send_broadcast} -u normal -i $ICON_batt_plugged \
  132.                         -T 10000 \
  133.                         -t "Stromversorgung hergestellt!" \
  134.                         -m "Ladestand: $((100*$CAPACITY/$MAX_BATTERY))%"
  135.  
  136.                 elif [ "$CAPACITY" -lt "$LOW_BATTERY" ] && \
  137.                      [ "$STATE" = "Discharging" ];
  138.                 then
  139.                     $($PLAY)
  140.                     ${notify_send_broadcast} -u normal \
  141.                         -T "$(($SLEEP/2*1000))" \
  142.                         -i "$ICON_batt_low" -t "Akku schwach" \
  143.                         -m "$((100*$CAPACITY/$MAX_BATTERY))% verbleibend,
  144. $ACTION bei $SUPERCRITICAL%"
  145.                 fi
  146.             fi
  147.             sleep $SLEEP
  148.         done
  149.  
  150. #    else
  151. #        echo "battery_warning seems to be running..."
  152. #        exit 1;
  153. #    fi;
  154.     return 0;
  155. }
  156.  
  157.  
  158. stop()
  159. {
  160.     processID="$(ps -eF|grep -v $$|grep -v 'grep'|grep bash|awk '/battery_warning/ {print $2}')"
  161.     if [ -f $PIDFILE ]; then
  162.         echo "$PIDFILE found. Killing battery_warning..."
  163.         kill `cat $PIDFILE` && rm $PIDFILE
  164.     elif [ -n "$processID" ]; then
  165.         echo "$PIDFILE not found, but battery_warning
  166. running. Killing battery_warning..."
  167.         kill $processID
  168.     else
  169.         echo "Process battery_warning not running.
  170. Nothing to do."
  171.     fi;
  172.     return 0;
  173. }
  174.  
  175. status()
  176. {
  177.     processID="$(ps -eF|grep -v $$|grep -v 'grep'|grep bash|awk '/battery_warning/ {print $2}')"
  178.     if [ -f $PIDFILE -o "x" != "x$processID" ];
  179.     then
  180.         echo "battery_warning seems to be running with
  181. process ID $processID."
  182.     else
  183.         echo "Process battery_warning not running."
  184.     fi;
  185.     return 0;
  186. }
  187.  
  188. case $1 in
  189.     start)
  190.         start
  191.         ;;
  192.     stop)
  193.         stop;
  194.         ;;
  195.     restart)
  196.         stop;
  197.         sleep 1;
  198.         start
  199.         ;;
  200.     status)
  201.         status;
  202.         ;;
  203.     *)    
  204.         echo "Usage: $0 {start|stop|restart|status}"
  205.         ;;
  206. esac;
  207.  
  208. exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement