howtos:check_battery_state_with_bash_script
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | howtos:check_battery_state_with_bash_script [02/12/2018 21:34] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Battery state script ====== | ||
+ | The idea with this script is to check whether a server running on a laptop has lost its power source and needs to shutdown nicely. | ||
+ | ===== Prerequisite ===== | ||
+ | I use the utility called " | ||
+ | Install acpi with this command: | ||
+ | < | ||
+ | sudo apt-get install acpi | ||
+ | </ | ||
+ | |||
+ | ===== The script ===== | ||
+ | The script is very simple and I've put some comments into it to clarify what I'm doing. | ||
+ | |||
+ | < | ||
+ | #!/bin/bash | ||
+ | |||
+ | # Check if we're running on battery - the output will contain the word " | ||
+ | # and generate one line thus putting the value " | ||
+ | state=`acpi -b|grep Discharging| wc -l` | ||
+ | |||
+ | # Next we retract the percentage value of remaining power | ||
+ | procent=`acpi -b|awk -F ", " ' | ||
+ | |||
+ | # If the power value is below 10% of battery capacity and we're running without power | ||
+ | # we shutdown the server | ||
+ | if [ $procent -lt 10 -a $state -eq 1 ] | ||
+ | then | ||
+ | logger " | ||
+ | sleep 2 | ||
+ | halt | ||
+ | fi | ||
+ | </ |
howtos/check_battery_state_with_bash_script.txt · Last modified: 02/12/2018 21:34 by 127.0.0.1