You are not logged in.

#1 2021-07-12 22:28:05

LuX
Member
From: France
Registered: 2010-06-14
Posts: 79

[SOLVED] systemd user service unable to intialize GTK

Hello!

I have written a systemd user service, which is supposed to display notifications by means of gxmessage. It works if I start it myself, but not if I enable it and restart.

Here is the service (it has authorizations 644):

$ cat /etc/systemd/user/rsnapshot-pc-notify.service
[Unit]
Description= Display notifications

[Service]
Type= simple
ExecStart= /usr/local/bin/rsnapshot-pc-notify

[Install]
WantedBy=default.target

I have tried to replace default.target by graphical.target, but it makes no difference (apparently).

The script that it calls is the following (authorizations 755):

$ cat /usr/local/bin/rsnapshot-pc-notify
#!/bin/bash

notif=/tmp/rsnapshot-pc-msg

while inotifywait -e modify $notif
do 
    /usr/bin/gxmesssage "$(cat $notif)"
done

If I start this service with 'systemctl --user rsnapshot-pc-notify.service' it works as expected: whenever the root modifies /tmp/rsnapshot-pc-msg, gxmessage pops up in the user session a window which displays the content of /tmp/rsnapshot-pc-msg.

But if I simply enable this service and restart, although the service is running, it doesn't succeed to display the gxmessage windows, and complains that it is "unable to initialize GTK".

$ systemctl --user status rsnapshot-pc-notify
● rsnapshot-pc-notify.service - Watch and display notifications of the rsnapshot-pc service
     Loaded: loaded (/etc/xdg/systemd/user/rsnapshot-pc-notify.service; enabled; vendor pres>
     Active: active (running) since Mon 2021-07-12 23:52:04 CEST; 17min ago
   Main PID: 372 (rsnapshot-pc-no)
      Tasks: 2 (limit: 2337)
     Memory: 880.0K
        CPU: 39ms
     CGroup: /user.slice/user-792400015.slice/user@792400015.service/app.slice/rsnapshot-pc->
             ├─372 /bin/bash /usr/local/bin/rsnapshot-pc-notify
             └─631 inotifywait -e modify /tmp/rsnapshot-pc-msg

juil. 12 23:52:05 seth rsnapshot-pc-notify[373]: Setting up watches.
juil. 12 23:52:05 seth rsnapshot-pc-notify[373]: Watches established.
juil. 12 23:52:04 seth systemd[364]: Started Watch and display notifications of the rsnapsho>
juil. 12 23:52:12 seth rsnapshot-pc-notify[373]: /tmp/rsnapshot-pc-msg MODIFY
juil. 12 23:52:12 seth rsnapshot-pc-notify[629]: Unable to init server: Could not connect: C>
juil. 12 23:52:12 seth rsnapshot-pc-notify[629]: gxmessage: unable to initialize GTK
juil. 12 23:52:12 seth rsnapshot-pc-notify[631]: Setting up watches.
juil. 12 23:52:12 seth rsnapshot-pc-notify[631]: Watches established.

The point is probably that the file /tmp/rsnapshot-pc-msg is modified by root (or equivalently by a script started from a systemd system service), not by the user. However, it is precisely the reason why I start this service with the --user option of systemctl, following the recommendations of this post on stackexchange.

What am I missing here?

Last edited by LuX (2021-07-13 16:41:26)

Offline

#2 2021-07-12 22:36:33

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] systemd user service unable to intialize GTK


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2021-07-13 07:02:57

LuX
Member
From: France
Registered: 2010-06-14
Posts: 79

Re: [SOLVED] systemd user service unable to intialize GTK

Thank you for your answer.

I had already read this wiki page (the stackexchange page which I have mentioned also refers to it). What I understand from it, is simply that it should work out of the box.

Indeed it says that DISPLAY and XAUTHORITY variables need to be set. They are: the former to ":0.0", the latter to "path_to_my_home_dir/.Xauthority" (even if I switch to root with 'su', this variable points to the .Xauthority file in my home dir).

Then it says that:

Systemd provides a script in /etc/X11/xinit/xinitrc.d/50-systemd-user.sh to import those variables into the systemd user session on X launch. So unless you start X in a nonstandard way, user services should be aware of the DISPLAY and XAUTHORITY.

This script is indeed in place. I log in with lightdm, I use Xorg, and my desktop is xfce. Here is my .xinitrc file:

source .profile

exec startxfce4

All this seems pretty standard to me, so I understand from the wiki that "user services should be aware... etc". Otherwise, the last sentence quoted above is misleading.

So why doesn't it work? Am I supposed to do something to run this script on boot? From a service? How should I write it? Should I copy this script into my home directory? Or load it from my .xinitrc?

--- EDIT ---
Please note that this problem occurs only when the service auto-starts after boot, from 'systemctl --user enable rsnapshot-pc-notify'. If I stop and restart it from the command line with:

$ systemctl --user stop rsnapshot-pc-notify
$ systemctl --user start rsnapshot-pc-notify

then it works!
------------

The wiki doesn't say a word on it, so I guess that none of these questions is relevant. In other words: I'm stuck.

Thus, could you expand your hint a little bit?

Last edited by LuX (2021-07-13 07:21:02)

Offline

#4 2021-07-13 07:56:14

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] systemd user service unable to intialize GTK

If that is the entirety of your .xinitrc, then it is missing key functionality. Please see the wiki page. But you say you use LightDM, which ignores .xinitrc?

Please check your session is not broken: https://wiki.archlinux.org/title/Genera … ermissions

Then, from boot when the service fails, paste the output of `journalctl _PID=XXXX`, where the PID is from `systemctl --user status rsnapshot-pc-notify.service`


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2021-07-13 08:13:28

LuX
Member
From: France
Registered: 2010-06-14
Posts: 79

Re: [SOLVED] systemd user service unable to intialize GTK

I have posted my .xinitrc file just in case of, but indeed I log in with lightdm.

I have restarted again, and run what you suggested (thanks for that). Note that /tmp/rsnapshot-msg exists and is non-empty, so its content should be displayed (but is not).

Here is the result: the session doesn't seem to be broken, and journalctl_PID doesn't output anything.   :-/

$ ls -l /tmp/rsnapshot-pc-msg 
-rw-r--r-- 1 root root 10 13 juil. 10:01 /tmp/rsnapshot-pc-msg

$ systemctl --user status rsnapshot-pc-notify.service
● rsnapshot-pc-notify.service - Watch and display notifications of the rsnapshot-pc service
     Loaded: loaded (/etc/xdg/systemd/user/rsnapshot-pc-notify.service; enabled; vendor pres>
     Active: active (running) since Tue 2021-07-13 10:00:53 CEST; 32s ago
   Main PID: 374 (rsnapshot-pc-no)
      Tasks: 2 (limit: 2337)
     Memory: 864.0K
        CPU: 40ms
     CGroup: /user.slice/user-792400015.slice/user@792400015.service/app.slice/rsnapshot-pc->
             ├─374 /bin/bash /usr/local/bin/rsnapshot-pc-notify
             └─637 inotifywait -e modify /tmp/rsnapshot-pc-msg

juil. 13 10:00:53 seth systemd[366]: Started Watch and display notifications of the rsnapsho>
juil. 13 10:01:03 seth rsnapshot-pc-notify[590]: Setting up watches.
juil. 13 10:01:03 seth rsnapshot-pc-notify[590]: Watches established.
juil. 13 10:01:10 seth rsnapshot-pc-notify[590]: /tmp/rsnapshot-pc-msg MODIFY
juil. 13 10:01:10 seth rsnapshot-pc-notify[635]: Unable to init server: Could not connect: C>
juil. 13 10:01:10 seth rsnapshot-pc-notify[635]: gxmessage: unable to initialize GTK
juil. 13 10:01:10 seth rsnapshot-pc-notify[637]: Setting up watches.
juil. 13 10:01:10 seth rsnapshot-pc-notify[637]: Watches established.

$ journalctl_PID=374

$ loginctl show-session $XDG_SESSION_ID
Id=2
User=792400015
Name=darniere
Timestamp=Tue 2021-07-13 10:00:52 CEST
TimestampMonotonic=12837044
VTNr=7
Seat=seat0
Display=:0
Remote=no
Service=lightdm
Desktop=xfce
Scope=session-2.scope
Leader=356
Audit=2
Type=x11
Class=user
Active=yes
State=active
IdleHint=no
IdleSinceHint=0
IdleSinceHintMonotonic=0
LockedHint=no

Last edited by LuX (2021-07-13 08:29:24)

Offline

#6 2021-07-13 08:18:25

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] systemd user service unable to intialize GTK

My bad, `journalctl --user _PID=XXXX` If you use SYSTEMD_LESS=FRXMK it won't truncate the lines.

And `loginctl show-session $XDG_SESSION_ID` returns nothing? That's fucked up...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#7 2021-07-13 08:32:12

LuX
Member
From: France
Registered: 2010-06-14
Posts: 79

Re: [SOLVED] systemd user service unable to intialize GTK

jasonwryan wrote:

And `loginctl show-session $XDG_SESSION_ID` returns nothing? That's fucked up...

Copy-paste error, sorry! It's fixed now in my previous post.

Here are the last lines of the output of 'journalctl --user _PID=374'.

-- Boot bd7efee8115b4ffcbffcaa257d6a894c --
juil. 11 09:27:06 seth dbus-daemon[374]: [session uid=792400015 pid=374] Activating via systemd: service name='org.gtk.vfs.Daemon' unit='gvfs-daemon.service' requested by ':1.4' (uid=792400015 pid=361 comm="xfce4-session ")
juil. 11 09:27:06 seth dbus-daemon[374]: [session uid=792400015 pid=374] Successfully activated service 'org.gtk.vfs.Daemon'
juil. 11 09:27:06 seth dbus-daemon[374]: [session uid=792400015 pid=374] Activating via systemd: service name='org.a11y.Bus' unit='at-spi-dbus-bus.service' requested by ':1.7' (uid=792400015 pid=361 comm="xfce4-session ")
juil. 11 09:27:06 seth dbus-daemon[374]: [session uid=792400015 pid=374] Successfully activated service 'org.a11y.Bus'
juil. 11 09:27:06 seth dbus-daemon[374]: [session uid=792400015 pid=374] Activating service name='org.xfce.Xfconf' requested by ':1.4' (uid=792400015 pid=361 comm="xfce4-session ")
juil. 11 09:27:06 seth dbus-daemon[374]: [session uid=792400015 pid=374] Successfully activated service 'org.xfce.Xfconf'
juil. 11 09:27:07 seth dbus-daemon[374]: [session uid=792400015 pid=374] Activating service name='org.freedesktop.thumbnails.Thumbnailer1' requested by ':1.17' (uid=792400015 pid=449 comm="xfdesktop ")
juil. 11 09:27:07 seth dbus-daemon[374]: [session uid=792400015 pid=374] Activating via systemd: service name='org.gtk.vfs.UDisks2VolumeMonitor' unit='gvfs-udisks2-volume-monitor.service' requested by ':1.18' (uid=792400015 pid=458 comm="/usr/lib/tumbler-1/tumblerd ")
juil. 11 09:27:08 seth dbus-daemon[374]: [session uid=792400015 pid=374] Successfully activated service 'org.gtk.vfs.UDisks2VolumeMonitor'
juil. 11 09:27:08 seth dbus-daemon[374]: [session uid=792400015 pid=374] Activating via systemd: service name='org.gtk.vfs.MTPVolumeMonitor' unit='gvfs-mtp-volume-monitor.service' requested by ':1.18' (uid=792400015 pid=458 comm="/usr/lib/tumbler-1/tumblerd ")
juil. 11 09:27:08 seth dbus-daemon[374]: [session uid=792400015 pid=374] Successfully activated service 'org.gtk.vfs.MTPVolumeMonitor'
juil. 11 09:27:08 seth dbus-daemon[374]: [session uid=792400015 pid=374] Activating via systemd: service name='org.gtk.vfs.GPhoto2VolumeMonitor' unit='gvfs-gphoto2-volume-monitor.service' requested by ':1.18' (uid=792400015 pid=458 comm="/usr/lib/tumbler-1/tumblerd ")
juil. 11 09:27:08 seth dbus-daemon[374]: [session uid=792400015 pid=374] Successfully activated service 'org.gtk.vfs.GPhoto2VolumeMonitor'
juil. 11 09:27:08 seth dbus-daemon[374]: [session uid=792400015 pid=374] Successfully activated service 'org.freedesktop.thumbnails.Thumbnailer1'
-- Boot 4f65cf99f0d94c18b30bccca7c12c2b4 --
juil. 12 22:52:43 seth rsnapshot-pc-notify[374]: Setting up watches.
juil. 12 22:52:43 seth rsnapshot-pc-notify[374]: Watches established.

Last edited by LuX (2021-07-13 08:45:24)

Offline

#8 2021-07-13 08:36:23

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] systemd user service unable to intialize GTK

Session is not broken, which is good. Can't read the journal though:

jasonwryan wrote:

If you use SYSTEMD_LESS=FRXMK it won't truncate the lines.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#9 2021-07-13 08:40:49

LuX
Member
From: France
Registered: 2010-06-14
Posts: 79

Re: [SOLVED] systemd user service unable to intialize GTK

Sorry, again.
Edited.

I'm surprised that the last lines are dated from yesterday, 22:52:43.

Last edited by LuX (2021-07-13 08:47:16)

Offline

#10 2021-07-13 08:50:47

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] systemd user service unable to intialize GTK

Well, after the last boot, it seems to have successfully run, so I'm at a loss... I run a --user service to send fnotify messages to dzen2 and I specify the display, and it works fine, from boot or started manually.

I have no other suggestions, other than to modify the script to a) write all output to a file, and b) include the environment details in that output. Then compare files for a manual run and one that doesn't work.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#11 2021-07-13 09:11:49

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,902
Website

Re: [SOLVED] systemd user service unable to intialize GTK

Mod note: moving to Apps & DEs


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

#12 2021-07-13 09:20:17

LuX
Member
From: France
Registered: 2010-06-14
Posts: 79

Re: [SOLVED] systemd user service unable to intialize GTK

jasonwryan wrote:

I run a --user service to send fnotify messages to dzen2 and I specify the display, and it works fine, from boot or started manually.

Would you mind to send me your service, so that I can test if it works for me?

jasonwryan wrote:

I have no other suggestions, other than to modify the script to a) write all output to a file, and b) include the environment details in that output.

Which environment details do you think that I should include?

Offline

#13 2021-07-13 11:07:00

seth
Member
Registered: 2012-09-03
Posts: 51,731

Re: [SOLVED] systemd user service unable to intialize GTK

LuX wrote:

Please note that this problem occurs only when the service auto-starts after boot, from 'systemctl --user enable rsnapshot-pc-notify'. If I stop and restart it from the command line with:

$ systemctl --user stop rsnapshot-pc-notify
$ systemctl --user start rsnapshot-pc-notify

then it works!

Race condition - the user service is run before the X11 server is up?
Do you autologin w/ lightdm?
Or do you log into a console session and start lightdm from there?

ExecStartPre=sleep 3

?

Online

#14 2021-07-13 13:36:46

LuX
Member
From: France
Registered: 2010-06-14
Posts: 79

Re: [SOLVED] systemd user service unable to intialize GTK

seth wrote:
ExecStartPre=sleep 3

?

Works! cool

Even 'ExecStartPre= sleep 1' is sufficient. Thank you very much. smile

To answer your questions: I neither autologin, nor start lightdm into a console session. The login manager is auto-started as a service by systemd, and I use it to login.

Maybe I will do some tests to see if some problems occur (very probably, I guess) if I login these different ways.

seth wrote:

Race condition - the user service is run before the X11 server is up?

I guess so. But why? Isn't 'WantedBy=default.target' (or better 'WantedBy=graphical.target') sufficient to avoid this from happening for user services? Is there another 'WantedBy=' or some other option (more precise than 'ExecStartPre= sleep N') which I should have set in the [Install] section of the service?

Offline

#15 2021-07-13 14:01:39

seth
Member
Registered: 2012-09-03
Posts: 51,731

Re: [SOLVED] systemd user service unable to intialize GTK

Lightdm imports the environment through /etc/X11/xinit/xinitrc.d/50-systemd-user.sh in its /etc/Xsession wrapper script - I guess that's run after creating the session and *while* systemd is doing its user service thing.
Ideally you'd have a conditional activation or be able to schedule the service, but sometimes it's much easier and sufficient to look the other way and randomly delay the action to get out of a race - hedgehog style ;-)

Online

Board footer

Powered by FluxBB