83 lines
1.7 KiB
Bash
Executable File
83 lines
1.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# 20260819 - Hyperling
|
|
# Alert the user every so often once the battery is low.
|
|
|
|
echo "`date` - Checking Active Programs"
|
|
|
|
ps -ef | grep battery_nag.sh | grep -v grep
|
|
active="$?"
|
|
if [[ "$active" == 0 ]]; then
|
|
echo "Program already running, exiting."
|
|
exit 1
|
|
fi
|
|
|
|
echo "`date` - Loading Functions"
|
|
|
|
which battery.function
|
|
source battery.function
|
|
|
|
echo "`date` - Parsing Parameters"
|
|
|
|
typeset -i alert sleep
|
|
|
|
# Default to 20%.
|
|
alert=20
|
|
if [[ -n "$1" ]]; then
|
|
echo -n "alert changed from '$alert' "
|
|
alert="$1"
|
|
echo "to '$alert'"
|
|
fi
|
|
|
|
# Default to 5 minutes.
|
|
sleep=300
|
|
if [[ -n "$2" ]]; then
|
|
echo -n "sleep changed from '$sleep' "
|
|
sleep="$2"
|
|
echo "to '$sleep'"
|
|
fi
|
|
|
|
echo "`date` - Alerting every ${sleep} seconds when under ${alert}%."
|
|
|
|
echo "`date` - Starting Loop"
|
|
|
|
while true; do
|
|
|
|
batt=`battery-display`
|
|
batt=${batt//%/}
|
|
|
|
if (( batt <= alert )); then
|
|
title="Battery is below ${alert}%!"
|
|
message="Currently at ${batt}%."
|
|
cmd="`which fyi 2>/dev/null || which notify-send 2>/dev/null`"
|
|
|
|
$cmd "$title" "$message"
|
|
status="$?"
|
|
|
|
if [[ "$status" == 0 ]]; then
|
|
echo -n "`date` - Success! "
|
|
else
|
|
echo -n "`date` - ERROR: "
|
|
fi
|
|
echo "Nag returned status ${status}."
|
|
else
|
|
echo "`date` - Battery at ${batt}%, did not nag."
|
|
fi
|
|
|
|
time=$sleep
|
|
if (( batt <= 2 )); then
|
|
echo "Battery under 2, 16x speed."
|
|
time=$(( sleep / 16 ))
|
|
elif (( batt <= 5 )); then
|
|
echo "Battery under 5, 8x speed."
|
|
time=$(( sleep / 8 ))
|
|
elif (( batt <= 8 )); then
|
|
echo "Battery under 8, 4x speed."
|
|
time=$(( sleep / 4 ))
|
|
elif (( batt <= 10 )); then
|
|
echo "Battery under 10, 2x speed."
|
|
time=$(( sleep / 2 ))
|
|
fi
|
|
echo "`date` - Sleeping for ${time}"
|
|
sleep $time
|
|
done
|