Skip to content

Instantly share code, notes, and snippets.

@LieBtrau
Last active July 7, 2024 13:09
Show Gist options
  • Save LieBtrau/8dc2ac537eea626b9e9f6d8fade0352e to your computer and use it in GitHub Desktop.
Save LieBtrau/8dc2ac537eea626b9e9f6d8fade0352e to your computer and use it in GitHub Desktop.
Log out users when laptop battery level gets too low
#!/bin/bash
# Cronjobs for each user can be edited with "crontab -e"
battery_level=$(cat /sys/class/power_supply/BAT0/capacity)
battery_level_threshold=35
if (( $battery_level > $battery_level_threshold )); then
echo $(date -u) ": Battery level ok:" $battery_level"%"
exit 0
fi
ac_power_present=$(cat /sys/class/power_supply/AC/online)
if ((ac_power_present == 1));then
echo $(date -u) ": AC-power present"
exit 0
fi
export DISPLAY=$(whoami)
export XAUTHORITY=/home/$(whoami)/.Xauthority
export XDG_RUNTIME_DIR=/run/user/$(id -u $(whoami))
/usr/bin/notify-send 'Laptopbatterij opladen nu!'
sleep 3
# Users still have the option here to cancel the logout
gnome-session-quit --logout
#Punishment when battery level is too low and power adapter not connected. No more cancel option.
#killall firefox
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment