Advertisement
Guest User

Untitled

a guest
Nov 18th, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Send a notification if the laptop battery is either low
  4. # or is fully charged.
  5.  
  6. export DISPLAY=:0
  7. export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
  8.  
  9. # Battery percentage at which to notify
  10. WARNING_LEVEL=20
  11. BATTERY_DISCHARGING=$(acpi -b | grep "Battery 0" | grep -c "Discharging")
  12. BATTERY_LEVEL=$(acpi -b | grep "Battery 0" | grep -P -o '[0-9]+(?=%)')
  13.  
  14. # Use two files to store whether we've shown a notification or not (to prevent multiple notifications)
  15. EMPTY_FILE=/tmp/batteryempty
  16. FULL_FILE=/tmp/batteryfull
  17.  
  18. # Reset notifications if the computer is charging/discharging
  19. if [ "$BATTERY_DISCHARGING" -eq 1 ] && [ -f $FULL_FILE ]; then
  20. rm $FULL_FILE
  21. elif [ "$BATTERY_DISCHARGING" -eq 0 ] && [ -f $EMPTY_FILE ]; then
  22. rm $EMPTY_FILE
  23. fi
  24.  
  25. # If the battery is charging and is full (and has not shown notification yet)
  26. if [ "$BATTERY_LEVEL" -gt 95 ] && [ "$BATTERY_DISCHARGING" -eq 0 ] && [ ! -f $FULL_FILE ]; then
  27. notify-send "Battery Charged" "Battery is fully charged." -i "battery" -r 9991
  28. touch $FULL_FILE
  29. # If the battery is low and is not charging (and has not shown notification yet)
  30. elif [ "$BATTERY_LEVEL" -le $WARNING_LEVEL ] && [ "$BATTERY_DISCHARGING" -eq 1 ] && [ ! -f $EMPTY_FILE ]; then
  31. notify-send "Low Battery" "${BATTERY_LEVEL}% of battery remaining." -u critical -i "battery-alert" -r 9991
  32. touch $EMPTY_FILE
  33. fi
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement