Here is what I just created as workaround to the not yet available graphical-session.target
(On my Kubuntu 16.04 system):
- Create a pseudo systemd user unit which brings the graphical-session.target up and down.
Create ~/.config/systemd/user/xsession.target
with following contents:
[Unit]
Description = Xsession up and running
BindsTo=graphical-session.target
Tell systemd about this new unit:
$> systemctl --user daemon-reload
- Create autostart and shutdown scripts which controls the
xsession.target
via the currently available mechanics of the Ubuntu 16.04 desktop.
Create ~/.config/autostart-scripts/xsession.target-login.sh
with following contents:
#!/bin/bash
if ! systemctl --user is-active xsession.target &> /dev/null
then
/bin/systemctl --user import-environment DISPLAY XAUTHORITY
/bin/systemctl --user start xsession.target
fi
Create ~/.config/plasma-workspace/shutdown/xsession.target-logout.sh
with following contents:
#!/bin/bash
if systemctl --user is-active xsession.target &> /dev/null
then
/bin/systemctl --user stop xsession.target
fi
Make the scripts executable:
$> chmod +x ~/.config/autostart-scripts/xsession.target-login.sh
$> chmod +x ~/.config/plasma-workspace/shutdown/xsession.target-logout.sh
Note: these two files are placed where KDE will pick them up for autostart and shutdown. The files maybe placed somewhere else for other desktop environments (e.g. Gnome) - but I don't know about those environments.
Note: This workaround lacks support of multi desktop sessions. It only handles the graphical-session.target
correctly as long as only one active X11 session is run on a machine (but that's the case for most of us linux users).
- Create your own systemd user units which depend on
graphical-session.target
and have them run cleanly while being logged in on your desktop.
As example @mkaito's unit should look like this:
[Unit]
Description=Redshift
PartOf=graphical-session.target
[Service]
ExecStart=/bin/redshift -l 28:-13 -t 5300:3300 -b 0.80:0.91 -m randr
Restart=always
(Don't forget to do a daemon-reload
after editing your units!)
- Reboot your machine, login and verfiy your units are started as expected
$> systemctl --user status graphical-session.target
● graphical-session.target - Current graphical user session
Loaded: loaded (/usr/lib/systemd/user/graphical-session.target; static; vendor preset: enabled)
Active: active since Don 2017-01-05 15:08:42 CET; 47min ago
Docs: man:systemd.special(7)
$> systemctl --user status your-unit...
At some future day (will it be Ubuntu 17.04?) my workaround become obsolete since the system will handle the graphical-session.target
correctly itself.
At that day just remove the autostart and shutdown script and also the xsession.target
- your custom user units may stay untouched and just work.
2If you can provide an example unit file, and explain how to have the unit be able to use DISPLAY and XAUTHORITY, I'll be happy to switch the accepted answer. – mkaito – 2016-09-27T23:10:15.497
@mkaito I'll look into that once Debian releases Stretch. I'm running Debian stable and was waiting until then to play with it more. – John Eikenberry – 2017-02-25T20:28:35.717
@mkaito At https://github.com/systemd/systemd/blob/v219/NEWS#L194 it says "An X11 session scriptlet is now shipped that uploads $DISPLAY and $XAUTHORITY into the environment of the systemd --user daemon if a session begins. This should improve compatibility with X11 enabled applications run as systemd user services."
– josch – 2018-04-03T22:48:48.747I would still like to see an example unit file, just to make it clear if there's anything special needed. – mkaito – 2018-04-11T08:16:30.613