#!/usr/bin/env bash # 20260819 - Hyperling # Alert the user every so often once the battery is low. echo "`date` - Checking Active Programs" PID="$$" ps -ef | grep battery_nag.sh | grep -v $$ | grep -v grep active=`ps -ef | grep battery_nag.sh | grep -v $$ | grep -vc grep` 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 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`" if [[ -z "$cmd" ]]; then echo "ERROR: No command found to send notification, exiting." echo "Please install 'fyi' or 'notify-send' to use this script." 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 (( 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 if (( time < 10 )); then time=10 fi echo "`date` - Sleeping for ${time}" sleep $time done