Essentially, I would like this comment into a working answer.
I know how to extract the battery percentage from How to check battery status using terminal?:
upower -i $(upower -e | grep BAT) | grep --color=never -E percentage|xargs|cut -d' ' -f2|sed s/%//
And how to pop-up a basic notification:
notify-send "battery low"
But how can I set up a (bash?) script to permanently monitor the output and send notification as per this pseudo-code:
if battery_status < 10%
then notify-send "battery low"
and put my system to suspended state sudo pm-suspend
Best Answer
Step one: make pm-suspend accessible to all users, no password asked
Do
sudo visudo
and add this line at the end of the file:yourusername ALL=NOPASSWD: /usr/sbin/pm-suspend
Source: How do I run specific sudo commands without a password?
Step two: create batwatch.desktop file:
This is the file that will launch automatically the monitoring script. The file must be stored in
$HOME/.config/autostart/
folder.Notice that the script is in my
/home/serg/bin
folder. You can use whatever folder you like, but for the sake of standards /usr/bin or /home/username/bin would be more prefered.Source: How to run a script on startup
Step three:create the actual script, save in the same location as Exec= line
Here's the actual script. Notice, I'm using bash there, but it also should work with korn shell. I added some comments, so read those to understand what the script does exactly
Step four: reboot and test if the script works
For this purpose you can adjust the number
[0-9]|11)
to whatever value you like, for example65)
to suspend at 65%. The you will suspend only if you're not connected to power supply (i.e, not charging).Let me know if you like this, and if it works, make sure to upvote and click the grey checkmark to the left side of my answer !
Cheers !