You are not logged in.

#1 2016-06-29 13:37:56

rbaj
Member
Registered: 2016-01-27
Posts: 36

[SOLVED] Battery notifications with dunst and notify-send

I'd like to get desktop notifications when my laptop battery is running low. I have dunst installed and this works fine for notifications from e.g. Network Manager. The script I'm using shows notifications if I run it manually but not when called by systemd. I can see it's getting called correctly from the logs, but nothing appears on my desktop.

check-battery.sh
---
!/bin/bash
printf 'script started' | systemd-cat -t check-battery #write to log when script is called
BATTINFO=`acpi -b`
if [[ `echo $BATTINFO | grep Discharging` && `echo $BATTINFO | cut -f 5 -d " "` < 00:30:00 ]] ; then
        printf 'condition is true' | systemd-cat -t check-battery #write to log if condition is true
        DISPLAY=:0 /usr/bin/notify-send -u low "battery" "$BATTINFO"
fi

How can I get the notifications to appear?
Thanks

Last edited by rbaj (2016-06-30 08:40:45)

Offline

#2 2016-06-29 14:22:57

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,302

Re: [SOLVED] Battery notifications with dunst and notify-send

This might help you: https://wiki.archlinux.org/index.php/Sy … XAUTHORITY

Is this script run from a systemd system service (as root) or from a systemd user service (as user)?

Offline

#3 2016-06-29 14:33:09

rbaj
Member
Registered: 2016-01-27
Posts: 36

Re: [SOLVED] Battery notifications with dunst and notify-send

The .service and .timer units are in /usr/lib/systemd/system/

The check-battery.sh script happens to be in ~/.config/systemd/user

echo $DISPLAY $XAUTHORITY gives sensible answers..

Maybe one of these is wrong:

$ cat check-battery-user.service

[Unit]
Description=check-battery

[Service]
ExecStart=/home/robaj/.config/systemd/user/check-battery.sh

[Install]
WantedBy=multi-user.target
$cat check-battery-user.timer

[Unit]
Description=Timer to check battery status

[Timer]
OnBootSec=1min
OnUnitActiveSec=1min

[Install]
WantedBy=multi-user.target

Offline

#4 2016-06-29 14:45:41

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [SOLVED] Battery notifications with dunst and notify-send

Try running it as a user service.


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#5 2016-06-29 21:12:36

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,302

Re: [SOLVED] Battery notifications with dunst and notify-send

To use graphical notifications in a systemd system service you need to pass the DISPLAY and XAUTHORITY variables to the service file like that:

[Unit]
Description=check-battery

[Service]
Environment="DISPLAY=:0" "XAUTHORITY=/home/robaj/.Xauthority"
ExecStart=/home/robaj/.config/systemd/user/check-battery.sh

[Install]
WantedBy=multi-user.target

You can remove the DISPLAY set before the notify-send command of the script.

Or run the timers/service as systemd user units.
Put them in ~/.config/systemd/user with these changes:

$ cat check-battery-user.service

[Unit]
Description=check-battery

[Service]
ExecStart=/home/robaj/.config/systemd/user/check-battery.sh
$cat check-battery-user.timer

[Unit]
Description=Timer to check battery status

[Timer]
OnActiveSec=1min
OnUnitActiveSec=1min

[Install]
WantedBy=timers.target

In this case there's no need to pass the DISPLAY and XAUTHORITY variables, because they are already set for the systemd user manager (so it's simpler).
There is a similar thread already here: https://bbs.archlinux.org/viewtopic.php?id=205867

Last edited by berbae (2016-06-29 21:19:32)

Offline

#6 2016-06-30 08:40:31

rbaj
Member
Registered: 2016-01-27
Posts: 36

Re: [SOLVED] Battery notifications with dunst and notify-send

I didn't spot that thread. This is very helpful, thanks.

Offline

Board footer

Powered by FluxBB