You are not logged in.

#1 2015-10-29 05:49:29

bremby
Member
Registered: 2011-12-20
Posts: 11

[SOLVED] getting DISPLAY variable in a systemd unit / delaying it

Hello,

I am trying to set up my wallpaper using feh. I've written a systemd unit just the way it is in the Arch wiki, but that didn't work. I kept changing it and trying to make it work, so this is what I have now:

[Unit]
Description=Wallpaper setup with feh
Wants=lightdm.service
After=default.target
After=lightdm.service

[Service]
Type=oneshot
Environment="DISPLAY=:0"
ExecStart=/bin/bash -c '/usr/bin/feh --bg-fill "/home/bremby/bg_earthrise.jpg"'

[Install]
WantedBy=default.target

Now running 'journalctl --user-unit feh-wallpaper.service' gives me this:

Oct 26 06:56:09 uranus systemd[678]: Starting Wallpaper setup with feh...
Oct 26 06:56:13 uranus bash[688]: Invalid MIT-MAGIC-COOKIE-1 keyfeh ERROR: Can't open X display. It *is* running, yeah?
Oct 26 06:56:13 uranus systemd[678]: feh-wallpaper.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Oct 26 06:56:13 uranus systemd[678]: Failed to start Wallpaper setup with feh.
Oct 26 06:56:13 uranus systemd[678]: feh-wallpaper.service: Unit entered failed state.
Oct 26 06:56:13 uranus systemd[678]: feh-wallpaper.service: Failed with result 'exit-code'.
Oct 26 08:27:20 uranus systemd[678]: Stopped Wallpaper setup with feh.

So the problem is feh is started too soon for X to be available, I think. So how do I force it to wait for X finishing its startup? Or how do I delay this unit to give X time to boot?
Also, should I use the /etc/X11/xinit/xinitrc.d/50-systemd-user.sh script? If so, how?
Note that this unit works fine when started manually after login and also when I reboot. It doesn't work when I startup the PC for the first time the next day. So it's obviously a timing issue.

Feel free to move this thread to another subforum.
Thank you.

Last edited by bremby (2015-10-29 08:29:09)

Offline

#2 2015-10-29 08:02:07

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED] getting DISPLAY variable in a systemd unit / delaying it

bremby wrote:

Also, should I use the /etc/X11/xinit/xinitrc.d/50-systemd-user.sh script? If so, how?

You seem to be using LightDM, which will source that (and other scripts in that directory) for you. This takes care of setting the $DISPLAY but then you override it with the Environment line.

But why a systemd unit? I'd put the feh command in ~/.xinitrc (when using startx) or (when using LightDM) in ~/.xsession or in some autostart script executed by your WM. If you want to keep the systemd service, put

systemctl --user start feh

in there. So you would not enable the service but start it explicitly.

Other issues with the unit:

  • It's a user unit. lightdm.service is a system unit. The Wants/After won't work like that. And if you log in through LightDM, your systemd user instance will never start before LightDM anyway (unless you enable lingering).

  • The After=default.target is also a little odd.

  • There's no need to run feh through bash. You'd only do that if you needed some kind of shell syntax inside the string passed to -c.

So you'd only need Type and ExecStart (and a Description is nice). If you have more units that should start after X, you can create, for example, xorg.target. Then instead of starting multiple units from your X startup script, you start this target and you add

[Install]
WantedBy=xorg.target

to those units and enable them, so they get pulled in by xorg.target.

Last edited by Raynman (2015-10-29 08:03:22)

Offline

#3 2015-10-29 08:27:42

bremby
Member
Registered: 2011-12-20
Posts: 11

Re: [SOLVED] getting DISPLAY variable in a systemd unit / delaying it

Raynman wrote:
bremby wrote:

Also, should I use the /etc/X11/xinit/xinitrc.d/50-systemd-user.sh script? If so, how?

You seem to be using LightDM, which will source that (and other scripts in that directory) for you. This takes care of setting the $DISPLAY but then you override it with the Environment line.

But why a systemd unit? I'd put the feh command in ~/.xinitrc (when using startx) or (when using LightDM) in ~/.xsession or in some autostart script executed by your WM. If you want to keep the systemd service, put

systemctl --user start feh

in there. So you would not enable the service but start it explicitly.

OK, I'll put it in the .xinitrc file. The reasons for the systemd unit was following the wiki and a forum discussion.

Other issues with the unit:

  • It's a user unit. lightdm.service is a system unit. The Wants/After won't work like that. And if you log in through LightDM, your systemd user instance will never start before LightDM anyway (unless you enable lingering).

  • The After=default.target is also a little odd.

  • There's no need to run feh through bash. You'd only do that if you needed some kind of shell syntax inside the string passed to -c.

So you'd only need Type and ExecStart (and a Description is nice). If you have more units that should start after X, you can create, for example, xorg.target. Then instead of starting multiple units from your X startup script, you start this target and you add

[Install]
WantedBy=xorg.target

to those units and enable them, so they get pulled in by xorg.target.

Again, sources of this unit are the previously linked wiki and forum discussion posts. Obviously I have no idea what I am doing with regards to unit files. :-)

Thank you very much for quick and clear response!

Offline

#4 2015-10-29 09:20:28

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: [SOLVED] getting DISPLAY variable in a systemd unit / delaying it

bremby wrote:

OK, I'll put it in the .xinitrc file. The reasons for the systemd unit was following the wiki and a forum discussion.

The wiki and forum post are using a systemd service because they are using a systemd timer to change the wallpaper every few seconds.

Offline

Board footer

Powered by FluxBB