On my Gentoo Linux with dunstify as a notification daemon and StumpWM as a window manager, I wrote the following bash-script that is meant to inform me about my battery status:
#!/bin/bash
#This script notifies you if your battery is under 80% and puts the laptop to sleep in 5min if it is under 50% and isn't plugged in within that time.
if ! [ -z "$(acpi -b | cut -c 25)" ] && [[ $(acpi -b | cut -c 25) -lt 8 ]] && ! [[ "$( acpi -b | cut -c 28)" == "%" ]]
then if ! [ -z "$(acpi -b | cut -c 25)" ] && [[ $(acpi -b | cut -c 25) -lt 5 ]] && ! [[ "$(acpi -b | cut -c 28)" == "%" ]]
then dunstify "Battery <50" "Battery under 50%, will sleep in 5min."
sleep 5m
if ! [ -z "$(acpi -b | cut -c 25)" ] && [[ $(acpi -b | cut -c 25) -lt 5 ]] && ! [[ "$(acpi -b | cut -c 28)" == "%" ]]
then sudo systemctl suspend
fi
else dunstify "Battery <80" "Battery under 80%."
fi
fi
This works when I trigger it manually. However, it's supposed to be periodically triggered by systemd every 10min or so and notify me if my battery is low. So I wrote the following systemd-service:
[Unit]
Description=Checks your battery.
RefuseManualStart=no
RefuseManualStop=yes
[Service]
Type=oneshot
ExecStart=/bin/battery
If the battery is above 80%, the service finishes without a problem, as it should. However, if it is below 80% it exits with an error. Looking at the log, I find the following:
░░ A start job for unit battery.service has begun execution.
░░
░░ The job identifier is 79311.
Jul 10 16:02:54 gentoolaptop battery[537360]: Connecting to D-Bus failed: Unable to autolaunc>Jul 10 16:02:54 gentoolaptop systemd[1]: battery.service: Main process exited, code=exited, s>░░ Subject: Unit process exited
░░ Defined-By: systemd
░░ Support: https://gentoo.org/support/
░░
░░ An ExecStart= process belonging to unit battery.service has exited.
I've been trying to run this as a user service, where I get this message, and as a system service, where I get told that XDISPLAY and XAUTHORITY isn't set. Why can't it send messages when triggered as a service?
dunstifycommand.dunstifyis in /bin. I've replaced the instantiations ofdunstifyin the script with/bin/dunstify, but I still get the same error. I don't have cron installed because I read that I should use systemd instead.echo $DBUS_SESSION_BUS_ADDRESSshow a path in /run/user (instead of a path in /tmp)?unix:path=/tmp/dbus-WBOz1ZLHcU,guid=8d36769f902e2b818b3b87f9667eb930~/.dbus/directory? 2) Do you have a/run/user/$UIDdirectory? 3) Doessystemctl --user show-environmentshow DBUS_SESSION_BUS_ADDRESS?