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.targetThe 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.
1 Answer
I don't think you can run Qt without X, so you need to run X. It won't start by default if a monitor is not detected.
I found this Ubuntu Forums thread (link to exact answer) that might help you get over the problem of not being able to connect a monitor:
I am not that great with Ubuntu (been using it for past 2 years), so there might be other options to do the same.
3I was having the exact same problem - I want to run X on a machine that (for now) doesn't have a monitor attached.
Using the intel driver, X was finding no monitors attached, and giving up:
(II) intel(0): Output VGA disconnected (WW) intel(0): No outputs definitely connected, trying again... (II) intel(0): Output VGA disconnected (WW) intel(0): Unable to find initial modes (EE) intel(0): No valid modes. (II) UnloadModule: "intel"I stumbled across a solution pieced together from several different threads on these and other forums.
First I needed to make a basic
xorg.conffile, in which I specified the VESA driver rather than the intel one (see bottom of post for myxorg.conf)This led to me seeing a different error:
(EE) VESA: Kernel modesetting driver in use, refusing to load (WW) Falling back to old probe method for vesa (EE) No devices detected.I then had to add
nomodesetto my GRUB boot options (used to be in/boot/grub/menu.lstbut now in/etc/default/grub)GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nomodeset"...and run
sudo update-grub.With some basic settings for the (non-existent) monitor, the result is that I get X running with no monitor plugged in, using the VESA driver, and with a decent resolution - which is fine for what I wanted it for.
The
xorg.conffile:Section "Monitor" Identifier "Monitor0" HorizSync 31-81 VertRefresh 56-75 EndSection Section "Device" Identifier "Card0" Driver "vesa" EndSection Section "Screen" Identifier "Screen0" Device "Card0" Monitor "Monitor0" EndSection