Skip to main content
Topic: [SOLVED]Trying to get a custom runit service to notify me battery is low (dunst) (Read 843 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

[SOLVED]Trying to get a custom runit service to notify me battery is low (dunst)

Hi all, I have this bash script below that I have turned into a runit service called batnotify, which works fine when I call it from the terminal (very essentially it notifies me if the battery level is lower than 15%). Unfortunately it doesn't seem to work as a runit service (it simply hits 15% and then turns off). I have tried removing the break statement in the while loop, I've tried calling exec or not, I've tried calling the notify-send and sleep and cat commands directly from /usr/bin and there has been no luck so far. Any insights into how I might get this working would be greatly appreciated:

Code: [Select]
#!/bin/sh
while [ true ] ; do
PWRLVL=$(cat /sys/class/power_supply/BAT1/capacity)
        if [ $PWRLVL -le 15 ] ; then
            exec notify-send "Battery is at 15%!!";
            break;
        else
            sleep 15s;
        fi
done

Re: Trying to get a custom runit service to notify me when battery is low (dunst)

Reply #1
Your power manager should do it for you automatically if configured properly.  What DE/WM are you using?

Re: Trying to get a custom runit service to notify me when battery is low (dunst)

Reply #2
The info from the x-session needs to be available to the service script.
So you might run this as a script at your x-session login:

Code: [Select]
echo 'export DBUS_SESSION_BUS_ADDRESS='$DBUS_SESSION_BUS_ADDRESS > /tmp/dsba
chmod +x /tmp/dsba

And in the service script run /tmp/dsba before sending out the notification

artist

Re: Trying to get a custom runit service to notify me when battery is low (dunst)

Reply #3
Your power manager should do it for you automatically if configured properly.  What DE/WM are you using?

I'm using bspwm, it doesn't come with a power manager daemon and what not.

The info from the x-session needs to be available to the service script.
So you might run this as a script at your x-session login:

Code: [Select]
echo 'export DBUS_SESSION_BUS_ADDRESS='$DBUS_SESSION_BUS_ADDRESS > /tmp/dsba
chmod +x /tmp/dsba

And in the service script run /tmp/dsba before sending out the notification

artist

This is helpful. I recall another thread here on the forums mentioning that dunst notifications like this usually relied on dbus and systemd, but this recommendation of yours might work. I'm not on my laptop atm, and won't be until I get back to work later tonight, but I'll implement these changes when I'm back on it.

In the meanwhile, to be clear, I should export the $DBUS_SESSION_BUS_ADDRESS in my .xinitrc, correct? I just use startx in my .zprofile/.bash_profile and call bspwm from .xinitrc and don't use a display/login manager.

Can I call chmod +x from my .xinitrc like that? I would think I would need root privileges to add executable privileges to a file in /tmp ... but I could be wrong, I've never done that before.

EDIT: It appears /tmp doesn't need root privileges! Good to know! Learning something every day. Thanks!

Could you lastly explain why dunst requires the DBUS_SESSION_BUS_ADDRESS?

Again, thanks for the info! Very helpful.

Re: Trying to get a custom runit service to notify me when battery is low (dunst)

Reply #4
Hi all, I have this bash script below that I have turned into a runit service called batnotify, which works fine when I call it from the terminal (very essentially it notifies me if the battery level is lower than 15%). Unfortunately it doesn't seem to work as a runit service (it simply hits 15% and then turns off). I have tried removing the break statement in the while loop, I've tried calling exec or not, I've tried calling the notify-send and sleep and cat commands directly from /usr/bin and there has been no luck so far. Any insights into how I might get this working would be greatly appreciated:

Code: [Select]
#!/bin/sh
while [ true ] ; do
PWRLVL=$(cat /sys/class/power_supply/BAT1/capacity)
        if [ $PWRLVL -le 15 ] ; then
            exec notify-send "Battery is at 15%!!";
            break;
        else
            sleep 15s;
        fi
done

In my opinion you don't need to make it a runit service ( and pointless since dunst depends on Xorg ), just start the shell script when your window manager starts, either in the .xinitrc file if you use startx(1) or whatever mechanism your window manager uses to start.

below is a part of my xinitrc file:

dunst &
$HOME/.bin/notify_battery.sh &

Re: Trying to get a custom runit service to notify me when battery is low (dunst)

Reply #5
In my opinion you don't need to make it a runit service ( and pointless since dunst depends on Xorg ), just start the shell script when your window manager starts, either in the .xinitrc file if you use startx(1) or whatever mechanism your window manager uses to start.

below is a part of my xinitrc file:

dunst &
$HOME/.bin/notify_battery.sh &



Thank you for that, and yes I've tried it from within my .xinitrc file where it works... but only on startup when the battery is at or below 15%. When I've left it on  with the battery hovering around 15% power, it fails to launch again once it drops from 16% to 15%, which is when I'd like dunst to notify me.

I believe this has to do with dunst relying on dbus (for some reason...), so I will be integrating both your and Artist's recommendations later on tonight and record my findings here. To be clear, my batnotify script now looks like so, and will be called from in my .xinitrc :

Code: [Select]
#!/bin/sh

echo 'export DBUS_SESSION_BUS_ADDRESS='$DBUS_SESSION_BUS_ADDRESS > /tmp/dsba;
chmod +x /tmp/dsba;
/tmp/dbsa;

while [ true ] ; do
PWRLVL=$(cat /sys/class/power_supply/BAT1/capacity)
        if [ $PWRLVL -le 15 ] ; then
            exec notify-send "Battery is at 15%!!";
            break;
        else
            sleep 15s;
        fi
done

Re: Trying to get a custom runit service to notify me when battery is low (dunst)

Reply #6
Code: [Select]
$sudo pacman -S xfce4-power-manager 
$xfce4-power-manager-settings

General->Appearance->State notifications: ON
System->On battery->Critical power: configure to meet your needs


Re: Trying to get a custom runit service to notify me when battery is low (dunst)

Reply #7

Thank you for that, and yes I've tried it from within my .xinitrc file where it works... but only on startup when the battery is at or below 15%. When I've left it on  with the battery hovering around 15% power, it fails to launch again once it drops from 16% to 15%, which is when I'd like dunst to notify me.

I believe this has to do with dunst relying on dbus (for some reason...), so I will be integrating both your and Artist's recommendations later on tonight and record my findings here. To be clear, my batnotify script now looks like so, and will be called from in my .xinitrc :

Code: [Select]
#!/bin/sh

echo 'export DBUS_SESSION_BUS_ADDRESS='$DBUS_SESSION_BUS_ADDRESS > /tmp/dsba;
chmod +x /tmp/dsba;
/tmp/dbsa;

while [ true ] ; do
PWRLVL=$(cat /sys/class/power_supply/BAT1/capacity)
        if [ $PWRLVL -le 15 ] ; then
            exec notify-send "Battery is at 15%!!";
            break;
        else
            sleep 15s;
        fi
done

Did you try running the script manually and getting debugging output so you know what is actually going on?

Just start your window manager normally, and run the script like this:

sh -x your-script.sh

-x means run the script with debugging output

If you want to run the script in your .xinitrc file and still get debugging output, make your script run like this:

sh -x your-script.sh >/home/$USER/script.log 2>&1

Re: Trying to get a custom runit service to notify me when battery is low (dunst)

Reply #8
Ok! Thanks go out to Artist and Lancia! I ended up putting the exporting of the DBUS_SESSION_BUS_ADDRESS into .xinitrc and then called my original script (documented in the original post). The following is the code set of bash instructions I inputted into .xinitrc:

Code: [Select]
# Notification for Laptop Battery
echo 'export DBUS_SESSION_BUS_ADDRESS='$DBUS_SESSION_BUS_ADDRESS > /tmp/dsba &
chmod +x /tmp/dsba &
/tmp/dsba &
/home/brian/scripts/batnotify &

This ended up doing the trick, notifying me when my battery hit 15% using the simple dunst utility.

Again, thanks so much for the help and insights guys! This forum and its community has been a great boon to me and I just wanted to express how grateful I am for everyone's help. Thank you!