#!/bin/bash
# Get the battery percentage for battery 0
battery0_percent=$(cat /sys/class/power_supply/BAT0/capacity)
# Get the battery percentage for battery 1
battery1_percent=$(cat /sys/class/power_supply/BAT1/capacity)
# Threshold level
threshold=10
# Check if either battery is less than 10%
if [ "$battery0_percent" -lt "$threshold" ] || [ "$battery1_percent" -lt "$threshold" ]; then
# Display a Zenity warning
zenity --warning --text "Battery level is below 10% on one or both batteries!"
fi
This script sends a warning when one of my batteries is below 10%.
How can I run this script in the background, on a Linux system, so it will notify me when it has to?
while true; do [...]; sleep 1; done(1 for checking battery level every 1 second, might be an overkill). Otherwise,./battery-script.sh &or usingxinitrcwill run on the background, but exit at the end of the scriptnotify-send --urgency=low --icon=battery "Low battery" "Battery level is below 10% on one or both batteries"instead of zenity to get a nicer display.