You are not logged in.
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
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
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
Offline
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
I didn't spot that thread. This is very helpful, thanks.
Offline