How to set a systemd unit to start after loading the desktop?
The first suggestion didn't worked for me. So I tried a workaround instead. I set my x0vncserver systemd unit as follows
[Unit]
Description=Remote desktop service (VNC)
After=multi-user.target
[Service]
Type=forking
User=user
ExecStart=/bin/sh -c '/usr/bin/x0vncserver -display :0 -rfbport 5900 -passwordfile /home/user/.vnc/passwd &'
[Install]
WantedBy=default.target
And then as the above service fails because it tries to load before the desktop:0 loads, I set a systemd timer unit as x0vncserver.timer
to run the x0vncserver.service
unit after a defined time considering the desktop loading time for my machine (with poor old config) like below
[Unit]
Description=x0vncserver timer
[Timer]
# Time to wait after booting before it run for first time
OnBootSec=2m
Unit=x0vncserver.service
[Install]
WantedBy=default.target
And then I activated the timer unit by systemctl enable x0vncserver.timer
and rebooted. This time it worked as my goal was to start the server without my manual intervention :).
After looking at the lack of answers that do not include some sort of additional workaround, I came up with this solution myself. The solution was in the unit file after all, I checked out the systemd.unit man file under "After=", which requires a certain target/service to be running before starting the current unit, the "Requires=" alone will start up the service along with its dependency simultaneously. Here is a quote from that manual:
If a unit foo.service requires a unit bar.service as configured with Requires= and no ordering is configured with After= or Before=, then both units will be started simultaneously and without any delay between them if foo.service is activated.
So if the x server and the x0vncserver start up at the same time, the x0vncserver will fail due to not being able to connect up to an initialized x server. I needed to specifically add my DM to the "Requires=" and "After=" to get this to work. Now that this is done, it works every time without any additional magic. You will need to replace the "nodm.service" entries with the specific display manager that you are using. The "Restart" lines are there just in case I decide to restart my session by logging out, which will terminate the X server and then restart it immediately (since I am using nodm). I don't know for sure, but I would think this would also apply when using a standard DM as the x server would be running for login, and then be terminated with the new user x session replacing it after successful login. The only side effects of this are the VNC session being disconnected and needing to be restarted, but I don't think there is any solution to that specific issue without further magic.
Here is my [email protected] file:
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target multi-user.target nodm.service
Requires=nodm.service
[Service]
Type=simple
ExecStart=/usr/bin/x0vncserver -display %i -rfbport 5900 -securitytypes none
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
As an additional note, I would suggest modifying the "-securitytypes" directive to something more secure, as I am just using this in a local network in which I am the only user and am not concerned with any security issues.