You are not logged in.

#1 2015-12-06 00:46:13

nbd
Member
Registered: 2014-08-04
Posts: 389

[SOLVED] Desktop notifications from a systemd service

I have a systemd service running a python script from root which calls "notify-send" for sending desktop notifications (my DE is XFCE). When this script is run in terminal, notifications are sent OK. From systemd service notifications are not seen. The DISPLAY environment is set to :0 in the service file. The service file is as follows:

[Unit]
Description=Forwarding journal log to tty12.
After=systemd-user-sessions.service,systemd-journald.service

[Service]
Type=simple
ExecStart=/usr/bin/python /mnt/lib/pybin/modules/forwjour.py
Restart=always
RestartSec=1
KillMode=process
IgnoreSIGPIPE=no
Environment="DISPLAY=:0"

[Install]
WantedBy=getty.target

The python code for calling "notify-send" is the following:

subprocess.call( "notify-send 'test message'", shell=True )

or

subprocess.call(["notify-send", "test message"])

In the terminal both variants work, in the service both don't. How notifications from a service can be enabled?

Last edited by nbd (2015-12-07 18:30:23)


bing different

Offline

#2 2015-12-06 03:38:27

mwillems
Member
Registered: 2014-08-09
Posts: 89

Re: [SOLVED] Desktop notifications from a systemd service

Calling notify-send as root will not display notifications on your user's session unless you export the DBUS_SESSION_BUS_ADDRESS for your user's session.  You either need to call notify-send as the user you're logged in as, or correctly identify the DBUS_SESSION_BUS_ADDRESS (programmatically because it can change) and then export that environment variable.  That's probably why it works in the terminal but not in your root service (because in the terminal you're calling it as your user with all the right environment variables in the terminal).  You can easily test whether that is the problem by running the script with sudo in the terminal (it should fail then).

Last edited by mwillems (2015-12-06 03:39:56)

Offline

#3 2015-12-06 03:58:08

nbd
Member
Registered: 2014-08-04
Posts: 389

Re: [SOLVED] Desktop notifications from a systemd service

mwillems wrote:

That's probably why it works in the terminal but not in your root service (because in the terminal you're calling it as your user with all the right environment variables in the terminal).  You can easily test whether that is the problem by running the script with sudo in the terminal (it should fail then).

Of course I run it in terminal from the root account. And the 'env' in the terminal from the root account shows the "DISPLAY=:0.0", but no DBUS_SESSION_BUS_ADDRESS, and nonetheless notify-send works OK.

DBUS_SESSION_BUS_ADDRESS on the user account is '/run/user/1000/bus'. If I call notify-send from the service as

DBUS_SESSION_BUS_ADDRESS=/run/user/1000/bus notify-send 'test message'

It doesn't work either. I tried the following:

su myUserAccount -c "notify-send test_message"

Also doesn't work in service, but in terminal from root works.

When notify-send is called from the service, in the journal the following message appears:

Registered Authentication Agent for unix-process:7330:1126103 (system bus name :1.34 [/usr/bin/pkttyagent --notify-fd 4 --fallback], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8)

Don't know if it can tell something useful.


bing different

Offline

#4 2015-12-06 14:27:03

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

Re: [SOLVED] Desktop notifications from a systemd service

Do you really need root access to run your python script?
If what it needs is only read access to the systemd journal, you could put your user in the systemd-journal group, and work only at the user level.
In this case a user service would be easier to use:

[Unit]
Description=Forwarding journal log to tty12.

[Service]
ExecStart=/usr/bin/python /mnt/lib/pybin/modules/forwjour.py
Restart=always
RestartSec=1
KillMode=process
IgnoreSIGPIPE=no

Type simple is the default, and the DISPLAY variable is already exported in the context of a user in graphical environment.

I tested notify-send launched in a python script started in a systemd user service, and it works.

Offline

#5 2015-12-06 15:37:02

nbd
Member
Registered: 2014-08-04
Posts: 389

Re: [SOLVED] Desktop notifications from a systemd service

berbae,

this script writes (forwards journal messages) to /dev/tty12, which was producing "Permission denied" error if written from non-root account.

Actually it looks a bit strange that root cannot do something as simple as notify-send, what non-roots can do easily. Root it's a user account after all.


bing different

Offline

#6 2015-12-07 14:07:41

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

Re: [SOLVED] Desktop notifications from a systemd service

First, have you seen https://wiki.archlinux.org/index.php/Sy … ev.2Ftty12 ?
So do you still need your script or does it accomplish something else?
I don't see what you intend to show with notify-send...

Second, root has not access to the xorg server launched at a user login, because it is protected by the xauth protocol.
So to access the xorg server, root needs to know the DISPLAY and XAUTHORITY values of the user which has entered graphical environment.

These values can be passed in the systemd service; so your OP systemd system service missed only the XAUTHORITY value:

[Unit]
Description=Forwarding journal log to tty12.
After=systemd-user-sessions.service,systemd-journald.service

[Service]
Type=simple
ExecStart=/usr/bin/python /mnt/lib/pybin/modules/forwjour.py
Restart=always
RestartSec=1
KillMode=process
IgnoreSIGPIPE=no
Environment="DISPLAY=:0" "XAUTHORITY=/home/<user name which has entered graphical environment>/.Xauthority"

[Install]
WantedBy=getty.target

I am not sure of the After= and WantedBy= lines, but this is another matter.

I tested this on my computer, and it works with the Environment= line above:
notify-send from a python script shows the message, under root starting the systemd service put in /etc/systemd/system folder.

Another way would have been to copy the $HOME/.Xauthority file into the /root home directory, but this is insecure in my opinion.

I think this should solve your problem, but is all this necessary?

Last edited by berbae (2015-12-07 22:03:41)

Offline

#7 2015-12-07 18:29:41

nbd
Member
Registered: 2014-08-04
Posts: 389

Re: [SOLVED] Desktop notifications from a systemd service

berbae,

many thanks, this solves the issue.


bing different

Offline

Board footer

Powered by FluxBB