-
Notifications
You must be signed in to change notification settings - Fork 0
/
battery-warn.sh
executable file
·70 lines (53 loc) · 1.89 KB
/
battery-warn.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
s is a simple battery warning script. #
# It uses i3's nagbar to display warnings. #
# #
# @author agribu #
#############################################
# lock file location
export LOCK_FILE=/tmp/battery_state.lock
# check if another copy is running
if [[ -a $LOCK_FILE ]]; then
pid=$(cat $LOCK_FILE | awk '{print $1}')
ppid=$(cat $LOCK_FILE | awk '{print $2}')
# validate contents of previous lock file
vpid=${pid:-"0"}
vppid=${ppid:-"0"}
if (( $vpid < 2 || $vppid < 2 )); then
# corrupt lock file $LOCK_FILE ... Exiting
cp -f $LOCK_FILE ${LOCK_FILE}.`date +%Y%m%d%H%M%S`
exit
fi
# check if ppid matches pid
ps -f -p $pid --no-headers | grep $ppid >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
# another copy of script running with process id $pid
exit
else
# bogus lock file found, removing
rm -f $LOCK_FILE >/dev/null
fi
fi
pid=$$
ps -f -p $pid --no-headers | awk '{print $2,$3}' > $LOCK_FILE
# starting with process id $pid
# set Battery
BATTERY=$(ls /sys/class/power_supply/ | grep '^BAT')
# set full path
ACPI_PATH="/sys/class/power_supply/$BATTERY"
# get battery status
STAT=$(cat $ACPI_PATH/status)
# get remaining energy value
REM=`grep "POWER_SUPPLY_ENERGY_NOW" $ACPI_PATH/uevent | cut -d= -f2`
# get full energy value
FULL=`grep "POWER_SUPPLY_ENERGY_FULL_DESIGN" $ACPI_PATH/uevent | cut -d= -f2`
# get current energy value in percent
PERCENT=`echo $(( $REM * 100 / $FULL ))`
# set error message
MESSAGE="AWW SNAP! I am running out of juice ... Please, charge me or I'll have to power down."
# set energy limit in percent, where warning should be displayed
LIMIT="10"
# show warning if energy limit in percent is less then user set limit and
# if battery is discharging
if [ $PERCENT -le "$(echo $LIMIT)" ] && [ "$STAT" == "Discharging" ]; then
DISPLAY=:0.0 /usr/bin/i3-nagbar -m "$(echo $MESSAGE)"
fi