3

I'm trying to use a systemd service to send a desktop notification using a .sh script with notify-send command. My script notif.sh is the following:

#!/bin/bash
notify-send "Hello World"

and my systemd service notifme.service is the next one:

[Unit]
Description=should launch a desktop notification

[Service]
User=user
Type=simple
ExecStart=/bin/bash /home/user/notif.sh
Environment="DISPLAY=:0" "XAUTHORITY=/home/user/.Xauthority"

When I start the service everything seems to work well except the notification is not displayed. The notification script works properly when I run the script manually. When I run systemctl status notifme.service the output is:

● notifme.service - should launch a desktop notification
     Loaded: loaded (/etc/systemd/system/notifme.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-02-13 14:39:00 CET; 39s ago
   Main PID: 15771 (bash)
      Tasks: 9 (limit: 18678)
     Memory: 3.5M
     CGroup: /system.slice/notifme.service
             ├─15771 /bin/bash /home/user/notif.sh
             ├─15773 notify-send Hello World
             ├─15780 dbus-launch --autolaunch=017e96ffe51b466384d899f21cbecdc5 --binary-syntax --close-stderr
             ├─15781 /usr/bin/dbus-daemon --syslog-only --fork --print-pid 5 --print-address 7 --session
             ├─15783 /usr/bin/dbus-daemon --syslog-only --fork --print-pid 5 --print-address 7 --session
             └─15784 /usr/bin/plasma_waitforname org.freedesktop.Notifications

feb 13 14:39:00 slimbook systemd[1]: Started should launch a desktop notification.
feb 13 14:39:00 slimbook dbus-daemon[15781]: [session uid=1000 pid=15779] AppArmor D-Bus mediation is enabled
feb 13 14:39:00 slimbook dbus-daemon[15781]: [session uid=1000 pid=15779] Activating service name='org.freedesktop.Notifications' requested by ':1.0' (uid=1000 pid=15773 comm="notify-send Hello World " label="unconfined")

Does anyone have any idea why the notification is not showing? Thanks in advance

biorubenfs
  • 643
  • 6
  • 15

1 Answers1

8

After asking a colleague, it seems that the DBUS_SESSION_BUS_ADDRESS variable is not available when script is run from service. Just adding the export command as follow to the script is enough:

#!/bin/bash
export DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS:-unix:path=/run/user/${UID}/bus}"
notify-send "Hello World"

Apparently, in the service file, Environment is not necessary.

biorubenfs
  • 643
  • 6
  • 15