Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ########################################################################
- # Name: battery_warning, Author: Henning Hollermann, #
- # laclaro@mail.com, http://laclaro.wordpress.com #
- # #
- # This program is free software: you can redistribute it and/or #
- # modify it under the terms of the GNU General Public License #
- # as published by the Free Software Foundation, either version 3 #
- # of the License, or (at your option) any later version #
- # #
- # This program is distributed in the hope that it will be useful, #
- # but WITHOUT ANY WARRANTY; without even the implied warranty of #
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
- # GNU General Public License for more details. #
- # #
- # You should have received a copy of the GNU General Public License #
- # along with this program. If not, see <http://gnu.org/licenses/>. #
- # #
- # Bash script to be run in background to notify the user(s) on #
- # critical battery level and to shut down the system if the charge #
- # level gets too low #
- # #
- # To send broadcast-messages to all users while running as root #
- # it uses the notify-send-broadcast script. This is available #
- # according to the terms of the GPL as well #
- # #
- ########################################################################
- # USER SETTINGS
- # sleep time for main loop
- SLEEP="60"
- # low battery in % (warning begins)
- LOW="15"
- # critical battery in % (execute action)
- CRITICAL="13"
- # Seconds to delay shutdown after critical battery level is reached
- MAX_DELAY="180"
- # on this level ACTION is executed
- SUPERCRITICAL="10"
- # action performed on SUPERCRITICAL
- # Note: executing user has to have permissions to do so
- ACTION="sudo /sbin/shutdown now -h"
- # notification sound command
- PLAY="paplay /usr/share/sounds/freedesktop/stereo/dialog-warning.oga"
- # ICONS to display
- ICON_supercritical=dialog-warning
- ICON_batt_low=battery-caution
- ICON_batt_plugged=gnome-power-manager
- # SYSTEM SETTINGS
- # Battery-Name and AC-Adapter-Name
- BAT="BAT0"
- AC="AC"
- # Files to be found in
- AC_ONLINE_FILE="/sys/class/power_supply/$AC/online"
- BAT_PRESENT_FILE="/sys/class/power_supply/$BAT/present"
- ENERGY_FULL_FILE="/sys/class/power_supply/$BAT/energy_full"
- ENERGY_NOW_FILE="/sys/class/power_supply/$BAT/energy_now"
- STATE_FILE="/sys/class/power_supply/$BAT/status"
- CHARGE_FILE="/sys/class/power_supply/$BAT/energy_now"
- # Script to send X-broadcast messages as root
- notify_send_broadcast="/usr/local/bin/notify-send-wall"
- #notify_send_broadcast="/usr/bin/notify-send"
- # file for the process ID
- # executing user has to have permissions to write this file
- PIDFILE="/var/run/battery_warning/pid"
- # END OF SETTINGS
- #[ ! -d /var/run/battery_warning/ ] && mkdir /var/run/battery_warning/
- start()
- {
- echo "Starting battery-warning...";
- if [ ! -e $PIDFILE ]; then
- if [ -w $PIDFILE ]; then
- echo $$ > $PIDFILE
- else
- echo "Warning: cannot create file $PIDFILE."
- fi
- fi
- MAX_BATTERY=$(cat $ENERGY_FULL_FILE)
- LOW_BATTERY=$(($LOW*$MAX_BATTERY/100))
- CRITICAL_BATTERY=$(($CRITICAL*$MAX_BATTERY/100))
- SUPERCRITICAL_BATTERY=$(($SUPERCRITICAL*$MAX_BATTERY/100))
- # Main Loop
- while [ true ]; do
- PRESENT=$(cat $BAT_PRESENT_FILE)
- AC_online=$(cat $AC_ONLINE_FILE)
- if [ "$PRESENT" = "1" ] && [ "$AC_online" = "0" ] ; then
- STATE=$(cat $STATE_FILE)
- CAPACITY=$(cat $ENERGY_NOW_FILE)
- if [ "$CAPACITY" -lt "$CRITICAL_BATTERY" ] && \
- [ "$STATE" = "Discharging" ];
- then
- DELAY=0; INTERVAL=10;
- while [ "$AC_online" = "0" ]; do
- if [ $CAPACITY -lt $SUPERCRITICAL_BATTERY ] || \
- [ $DELAY -ge $MAX_DELAY ]; then
- ${notify_send_broadcast} -u critical \
- -i $ICON_supercritical \
- -T 15000 -t 'Achtung' \
- -m 'Das System wird heruntergefahren!'
- sleep 3
- $($ACTION)
- [ $? -ne 0 ] && ${notify_send_broadcast} -u critical \
- -i $ICON_supercritical \
- -m 'DAS SYSTEM KONNTE NICHT HERUNTERGEFAHREN
- WERDEN. SCHALTEN SIE DEN COMPUTER SOFORT AUS UM DATENVERLUST ZU VERHINDERN!' \
- -T 0 -t 'FEHLER'
- else
- ${notify_send_broadcast} -u normal \
- -i $ICON_batt_low \
- -m "Sehr niedriger Ladestand
- ($((100*$CAPACITY/$MAX_BATTERY))%). Das System wird in
- $(($MAX_DELAY-$DELAY+$INTERVAL)) Sekunden oder bei einem Ladestand
- unter $SUPERCRITICAL% automatisch heruntergefahren." \
- -T $((1000*$INTERVAL-200)) -t "Achtung"
- fi
- sleep $INTERVAL
- DELAY=$(($DELAY+$INTERVAL))
- CAPACITY=$(cat $ENERGY_NOW_FILE)
- AC_online=$(cat $AC_ONLINE_FILE)
- done
- ${notify_send_broadcast} -u normal -i $ICON_batt_plugged \
- -T 10000 \
- -t "Stromversorgung hergestellt!" \
- -m "Ladestand: $((100*$CAPACITY/$MAX_BATTERY))%"
- elif [ "$CAPACITY" -lt "$LOW_BATTERY" ] && \
- [ "$STATE" = "Discharging" ];
- then
- $($PLAY)
- ${notify_send_broadcast} -u normal \
- -T "$(($SLEEP/2*1000))" \
- -i "$ICON_batt_low" -t "Akku schwach" \
- -m "$((100*$CAPACITY/$MAX_BATTERY))% verbleibend,
- $ACTION bei $SUPERCRITICAL%"
- fi
- fi
- sleep $SLEEP
- done
- # else
- # echo "battery_warning seems to be running..."
- # exit 1;
- # fi;
- return 0;
- }
- stop()
- {
- processID="$(ps -eF|grep -v $$|grep -v 'grep'|grep bash|awk '/battery_warning/ {print $2}')"
- if [ -f $PIDFILE ]; then
- echo "$PIDFILE found. Killing battery_warning..."
- kill `cat $PIDFILE` && rm $PIDFILE
- elif [ -n "$processID" ]; then
- echo "$PIDFILE not found, but battery_warning
- running. Killing battery_warning..."
- kill $processID
- else
- echo "Process battery_warning not running.
- Nothing to do."
- fi;
- return 0;
- }
- status()
- {
- processID="$(ps -eF|grep -v $$|grep -v 'grep'|grep bash|awk '/battery_warning/ {print $2}')"
- if [ -f $PIDFILE -o "x" != "x$processID" ];
- then
- echo "battery_warning seems to be running with
- process ID $processID."
- else
- echo "Process battery_warning not running."
- fi;
- return 0;
- }
- case $1 in
- start)
- start
- ;;
- stop)
- stop;
- ;;
- restart)
- stop;
- sleep 1;
- start
- ;;
- status)
- status;
- ;;
- *)
- echo "Usage: $0 {start|stop|restart|status}"
- ;;
- esac;
- exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement