#!/usr/bin/env bash # 20260819 - Hyperling # Alert the user every so often once the battery is low. ## Setup ## echo "`date` - Checking Active Programs" PID="$$" ps -ef | grep battery_nag.sh | grep -v grep | grep -v killall | grep -v $$ active=`ps -ef | grep battery_nag.sh | grep -v grep | grep -v killall | grep -vc $$` if (( "$active" >= 1 )); then echo "Program already running, exiting. Number of processes: $active" exit 1 fi echo "`date` - Loading Functions" which battery.function || { echo "ERROR: Battery function not found, exiting." exit 2 } source battery.function ## Params ## 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 # Default to true. accelerate="Y" if [[ -n "$3" ]]; then echo -n "accelerate changed from '$accelerate' " accelerate="$3" echo "to '$accelerate'" fi echo "Alerting when under ${alert}%." echo "Notifying every ${sleep} seconds." if [[ "$accelerate" == "Y" ]]; then echo -n "Speeding up notifications as % gets lower." else echo -n "Notifcations staying persistent." fi echo " (accelerate == $accelerate)" ## Main ## 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`" if [[ -z "$cmd" ]]; then echo "ERROR: No command found to send notification, exiting." echo "Please install 'fyi' or 'notify-send' to use this script." echo "If not using a DE, also ensure 'dunst' or similar is running." exit 3 fi $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 [[ "$accelerate" == "Y" ]]; then if (( batt > 0 )); then time=$(( time / (alert / batt) )) else time=10 fi fi # Showing a notification every 10 seconds makes it persistent. # Any more than that casues multiple to stack which is unnecessary. if (( time < 10 )); then time=10 fi echo "`date` - Sleeping for ${time}." sleep $time done