18

There are programs that I want to automatically start after a successful graphical desktop login (e.g. to Gnome or Xmonad) and stop when I logout. How can I change the ~/.config/systemd/user/synergys.service file to do the above properly?

Right now with the service enabled, according to journalctl --user, the service tries to start when I login but fails with

Invalid MIT-MAGIC-COOKIE-1...

which I think basically means it failed to get a display. When I manually start it after logging in, it's fine. I suspect the service is starting too early, before there's a display, because WantedBy=default.target is not specific to a GUI login and even a text login will trigger it.

I've experimented with graphical-session.target but that went nowhere. This is on Ubuntu 20.04.

[Unit]
Description=Synergy Server

[Service]
ExecStart=/usr/bin/synergys --no-daemon --no-tray --address :: --serial-key XXXX --enable-crypto
Restart=always
RestartSec=3

[Install]
WantedBy=default.target

I know I can manually run the program as a desktop session 'startup' program, but this way does not provide automatic retry upon failure.

2
  • Can you explain how you experimented with graphical-session.target? Was it PartOf=graphical-session.target? How did it fail? Commented May 7, 2020 at 8:11
  • 3
    I added PartOf=graphical-session.target under [Unit] and removed [Install]. This worked for a Gnome login session but not for an xmonad login session, where the service did not start. systemctl --user list-units --type target shows a graphical-session.target under Gnome but not under xmonad. I'm looking for a way that works for different types of sessions and I don't know how graphical-session.target works. These sessions are started from lightdm. Commented May 13, 2020 at 11:06

2 Answers 2

3

Did you try the below?

[Unit]
Description=Synergy Server
After=graphical-session.target
BindsTo=graphical-session.target
PartOf=graphical-session.target
Requisite=graphical-session.target
ConditionEnvironment=DISPLAY
ConditionEnvironment=XAUTHORITY

[Service]
ExecStart=/usr/bin/synergys --no-daemon --no-tray --address :: --serial-key XXXX --enable-crypto
Restart=always
RestartSec=3

[Install]
WantedBy=graphical-session.target
  1. systemctl --user daemon-reload
  2. systemctl --user enable --now <the service unit>
  3. If (2) worked; Log-out from, then Log-in again into, the GUI to check it is automatically started.

@Syncopated

This worked for a Gnome login session but not for an xmonad login session, where the service did not start.

You should check your setup of xmonad if it integrates properly with systemd, especially display-manager.service being a symlink to it, see: man systemd.special.


PS:

  • I know some lines under [Unit] might be unneeded but i just put them there to be extra explicit...
  • If this doesn't work then im out of ideas as well 😉
5
  • Is that how xmonad is supposed to be added? I just made a /usr/share/xsessions/xmonad.desktop for the login menu to have an entry for xmonad (in addition to Gnome). I did not do anything with display-manager.service. Commented Oct 1, 2023 at 15:18
  • I have no idea what xmonad is or how to configure it, my advise is from a systemd point of view, as you can read in the man page i mentioned.... Commented Oct 2, 2023 at 9:41
  • There is a /usr/share/xsessions/xmonad.desktop and there is a /usr/share/xsessions/ubuntu.desktop. The former tries to execute xmonad and the latter tries to execute gnome-shell. xmonad is just another GUI desktop environment, just like gnome-shell. Both files are used by the login screen to provide the desktop environment options. Commented Oct 3, 2023 at 14:44
  • Where can I put this unit as a user? Commented Mar 11 at 8:22
  • 1
    @Bananguin Please consult the man pages before asking questions that are already properly documented in the man pages. Commented Mar 11 at 19:56
1

I had a similar use case where I wanted to register an X11 service that runs after the graphical session, where the full X11 stack has been started before my user service should run.

X11 programs require that both the DISPLAY and XAUTHORITY environment variables are set in the service's environment or they will fail to start. Unfortunately it is not well explained which systemd targets expose these environment variables.

On my GNOME desktop I was able to create a service by specifying that my service is wanted by the graphical-session.target

[Install]
WantedBy=graphical-session.target

See also: https://superuser.com/q/759759/194998

1
  • To check the environment that is exposed to the processes of your user you can use systemctl --user show-environment, see the man page of systemctl Commented Sep 30, 2023 at 8:13

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.