I need to run a Qt GUI App on every system start and run it forever. I have created a system service for it.
Here is the service file content:
QtGUIAPP.service
[Unit]
Description=QtGUIAPP
[Service]
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/user/.Xauthority"
ExecStart=/usr/bin/sudo /home/user/QtGUIAPP --no-sandbox
Restart=always
[Install]
WantedBy=multi-user.target
The above works fine without any problem.
But when I boot the PC without a monitor and access it remotely, the application seems not to be running. And I get an error in syslog like
Invalid MIT-MAGIC-COOKIE-1 keyQStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'QXcbConnection: Could not connect to display :0Could not connect to any X display.
Oct 18 12:45:55 user systemd[1]: QtGUIAPP.service: Main process exited, code=exited, status=1/FAILURE
Oct 18 12:45:55 user systemd[1]: QtGUIAPP.service: Unit entered failed state.
Oct 18 12:45:55 user systemd[1]: QtGUIAPP.service: Failed with result 'exit-code'.
Oct 18 12:45:55 user avahi-daemon[799]: Host name conflict, retrying with user-2
Oct 18 12:45:55 user avahi-daemon[799]: Registering new address record for 10.0.7.15 on enp2s0.IPv4.
Oct 18 12:45:56 user systemd[1]: QtGUIAPP.service: Service hold-off time over, scheduling restart.
I think the problem is with the line
Environment="DISPLAY=:0"
How can I change the above script to make it work both when a display is connected and not connected.