#!/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?
You have 2 ways to do this:
Add an ampersand after the command
./battery-script.sh &
This will keep the command running even after you close the terminal, by spawning a subprocess within your shell
If you want something that persists even after you log out the shell, you can add nohup before the command
nohup ./battery-script.sh
This will also redirect the output of the command into a file called "nohup.out"
If you want to go even further and run this script at startup, you also have a few different options:
If you want this script to start up every time you start your display server:
With x11:
Add the line ./battery-script.sh &
to the end of your ~/.xinitrc
for the script to be run every time you start up x11(probably what you want in this scenario, given how you have a line for a zenity warning)
If you run wayland, it depends on your wm, with sway it's .config/sway/config.d/autostart_applications
If you want to run the script before your display server even starts, you can put the script in /etc/profile.d
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments