You are not logged in.

#1 2019-11-03 10:41:55

Lazzu
Member
Registered: 2012-10-27
Posts: 62

[SOLVED] Getting desktop notifications from systemd service

Hi!

I'm trying to figure out why I am not getting desktop notifications that are being thrown from a service that is run by the user. Here's my .service:

[Unit]
Description=Checks for AUR updates
Wants=network-online.target graphical.target
After=network-online.target graphical.target

[Service]
ExecStart=/home/lasse/scripts/checkaurupdates.sh
Type=oneshot
User=lasse
Group=users
Environment="DISPLAY=:0" "XAUTHORITY=/home/lasse/.Xauthority"

[Install]
RequiredBy=multi-user.target

checkaurupdates.sh:

#!/bin/bash

AUR_UPDATES_AVAILABLE="$(yay -Qu | wc -l)"

if [ "$AUR_UPDATES_AVAILABLE" != "0" ]; then
    notify-send "System updates" "There are $AUR_UPDATES_AVAILABLE AUR updates available" -u critical -i system-updated
fi

Edit: Accidentally pressed enter in a wrong spot... here's the rest of my post:

As you can see I am running the service as a user but it still does not work. From the journal I can see the script is being run. I also tried setting the Environment to set the display and xauthority, but the notifications are still not visible.

Any directions where I should be look in to?

Last edited by Lazzu (2019-11-03 11:32:45)

Offline

#2 2019-11-03 11:17:21

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,406

Re: [SOLVED] Getting desktop notifications from systemd service

You are running the service as a system service "as your user", but that doesn't give you access to your user session (environment variable wise, most importantly, you don't have access to your user's DBUS adress which is the most important one in a notification sending context). Don't run it as a system service, use a proper User service instead

So in practice get rid of the user,group,wants,after and environment line, change RequiredBy=default.target  put your service file to an user service path (e.g. ~/.config/systemd/user/ ) and invoke it with

systemctl --user start  checkaurupdates.service

from your own user

You probably also want to add a timer unit to that, as even if your original service file worked, graphical.target and network-online.target would usually be fulfilled way before your desktop session and hence a notification daemon are running.

Last edited by V1del (2019-11-03 11:20:45)

Offline

#3 2019-11-03 11:32:17

Lazzu
Member
Registered: 2012-10-27
Posts: 62

Re: [SOLVED] Getting desktop notifications from systemd service

Thank you! That did the trick.

Offline

Board footer

Powered by FluxBB