dev-resources.site
for different kinds of informations.
Code. Battery notifier.
Published at
12/12/2024
Categories
bash
polybar
notifications
unixporn
Author
axkira
Main Article
Author
6 person written this
axkira
open
Out of frustration from some-battery-monitor-util sometimes missing the moment to notify me about low charge, this simple script was put together.
It checks the current status and level of the BAT0
device and does one of the following:
- notify about low battery level (15%)
- notify about critical battery level (5%)
- notify about near-zero battery level (3%), wait a couple of seconds and hibernate if battery status haven't changed to
Charging
Requisites:
- some notification daemon (I like
dunst
) -
systemd
for hibernation - something, that calls that script periodically (I call it with
polybar
every 60 seconds)
The script itself (battery-notifier.sh
):
#!/usr/bin/env bash
LOW_LEVEL=15
CRITICAL_LEVEL=5
HIBERNATE_LEVEL=3
LEVEL=$(cat /sys/class/power_supply/BAT0/capacity)
STATUS=$(cat /sys/class/power_supply/BAT0/status)
hibernate() {
notify-send -a "Battery" "Hibernating!" -u critical
# Give some time to plug the power in
sleep 5
# Hibernate if still discharging
STATUS=$(cat /sys/class/power_supply/BAT0/status)
if [[ "$STATUS" = "Discharging" ]]; then
systemctl hibernate
else
notify-send -a "Battery" "Power was plugged in. Hibernation is cancelled!"
fi
}
notify_critical() {
notify-send -a "Battery" "Battery is critically low!" -u critical
}
notify_low() {
notify-send -a "Battery" "Battery is low!"
}
if [[ "$STATUS" = "Discharging" ]]; then
if [[ "$LEVEL" -le "$HIBERNATE_LEVEL" ]]; then
hibernate
elif [[ "$LEVEL" -le "$CRITICAL_LEVEL" ]]; then
notify_critical
elif [[ "$LEVEL" -le "$LOW_LEVEL" ]]; then
notify_low
fi
fi
The polybar module config:
[module/battery_notifier]
type = custom/script
exec = ~/.config/polybar/battery-notifier.sh
interval = 60
* Notice, that script is put to ~/.config/polybar/
.
bash Article's
30 articles in total
Understanding Linux Shells: Interactive, Non-Interactive, and RC Files
read article
Fixing Linux Backup Sync Issues for exFAT Compatibility
read article
Ergonomic Pyhon Text Piping Solution for Linux Shell with pypyp and uv
read article
Changing the Login Shell with Security and Interactivity
read article
Final Bash Script Series Mastering Remote Server Management and Web App Deployment
read article
Writting a GH extension with AI
read article
Test GPIO pins on BeagleBone Black by toggling high to low
read article
NYP Infosec December CTF 2024 writeup!
read article
Building a Smart Heater Controller with Python, Docker, and Bluetooth #3
read article
The Complete Guide to Bash Commands
read article
How to Get Started with Bash Scripting for Automation
read article
Building a Basic Testing Framework in Bash 🐚
read article
Funny how, as I was writing this, I started wondering—could we make this pipeline even smarter? That’s when SonarQube came to mind. But can it even work with Bash scripts? I’d love to hear your thoughts!
read article
Domina Bash con ejemplos prácticos de Git
read article
Explaining Null Device or Black Hole Register in Vim
read article
A Media Server on Steroids - Walkthrough
read article
From FZF file preview to a browser for cht.sh to discovering the ideal solution
read article
Code. Battery notifier.
currently reading
🌟Simplifying User Account Management with Bash Scripting Day3🌟
read article
Become a Bash Scripting Pro in 10 Minutes: A Quick Guide for Beginners
read article
Automatically Monitor and Manage RAM Usage for Next.js Development
read article
Bash Script - Task Management
read article
Bash Your Tools
read article
Bash Script Series: Automating Log Analysis with Bash Script or Shell Script
read article
The Most Interesting Mostly Tech Reads of 2024
read article
🚀 Automating Process Monitoring & Restarting with Bash Scripting Day4! 🔧
read article
🚀 RazzShell v1.0.1 is Here: Plugin Support, Enhanced Job Management, and More! 🌟
read article
Step-by-Step Guide: Assigning a Namecheap Domain to DigitalOcean Hosting with Nginx
read article
Simplify GitHub Workflow Management with My-GitHub-Manager
read article
How to play a bunch of lemmings
read article
Featured ones: