Skip to content

Instantly share code, notes, and snippets.

@RealYukiSan
Last active April 17, 2024 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RealYukiSan/7baa2fad355d18060a206951c26dadd7 to your computer and use it in GitHub Desktop.
Save RealYukiSan/7baa2fad355d18060a206951c26dadd7 to your computer and use it in GitHub Desktop.
auto shutdown on low battery
#!/bin/sh
threshold=10 # threshold percentage to trigger alert
while :; do
capacity=$(cat /sys/class/power_supply/BAT1/capacity)
# If battery is discharging with capacity below threshold
if [ "${capacity}" -lt ${threshold} ];then
# Send a notification that appears for 300000 ms (5 min)
notify-send -t 300000 "Charge your battery!"
break
else
sleep 1m
fi
done
# the script are modified version of this article https://ejmastnak.com/tutorials/arch/battery-alert/
#!/bin/sh
while :; do
batt=`cat /sys/class/power_supply/BAT0/capacity`
if [[ $batt -le 10 ]]; then
shutdown now
fi
sleep 1m
done
@RealYukiSan
Copy link
Author

I wonder how to use other notification UI, says Telegram? so maybe I don't need to install additional package and use the telegram notification instead? is that even possible?

I see the telegram service on /usr/share/dbus-1/services/

@RealYukiSan
Copy link
Author

RealYukiSan commented Apr 17, 2024

it seems interesting to run the program on startup, maybe I need to mess up with systemd? haven't tried it before.

and watching the state of battery (charging or discharging) to resume/respawn the program on discharge, and exit/suspend/stop the program on charging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment