Starting QT app as a Systemd service

Hello Community,

I need to start my QT application without LXDE. I have disabled the lxdm.service and created a custom one that starts the X server only.

xserver.service:

[Unit]
Description=X Server
Conflicts=getty@tty1.service plymouth-quit.service
After=systemd-user-sessions.service getty@tty1.service plymouth-quit.service

[Service]
ExecStart=/usr/bin/X
ExecStop=/usr/bin/killall -9 X
Restart=always
IgnoreSIGPIPE=no

[Install]
Alias=display-manager.service

It works perfectly. The other service is the QT application itself:

qtapp.service:

[Unit]
Description=QT Application
After=xserver.service

[Service]
Type=simple
ExecStart=/opt/TestQtApp
ExecStop=/bin/bash -c 'pkill TestQtApp'
Restart=always
IgnoreSIGPIPE=no

[Install]
Alias=qtapp.servic

However, it won’t start due to some enviroment issue. The log in the journal looks like this:

Jan 07 14:58:57 apalis-imx6 systemd[1]: Started QT Application.
Jan 07 14:58:57 apalis-imx6 TestQtApp[607]: QML debugging is enabled. Only use this in a safe environment.
Jan 07 14:58:57 apalis-imx6 TestQtApp[607]: QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'
Jan 07 14:58:57 apalis-imx6 TestQtApp[607]: qt.qpa.screen: QXcbConnection: Could not connect to display
Jan 07 14:58:57 apalis-imx6 TestQtApp[607]: Could not connect to any X display.
Jan 07 14:58:57 apalis-imx6 systemd[1]: qtapp.service: Main process exited, code=exited, status=1/FAILURE
Jan 07 14:58:57 apalis-imx6 systemd[1]: qtapp.service: Unit entered failed state.
Jan 07 14:58:57 apalis-imx6 systemd[1]: qtapp.service: Failed with result 'exit-code'.
Jan 07 14:58:57 apalis-imx6 systemd[1]: qtapp.service: Service hold-off time over, scheduling restart.
Jan 07 14:58:57 apalis-imx6 systemd[1]: Stopped QT Application.
Jan 07 14:58:57 apalis-imx6 systemd[1]: Started QT Application.

May someone please advise what I could have done wrong?

Hello @vmetodiev,
What version of our BSP are you using? I ask because our current BSP 5.x doesn’t include xserver anymore, now we only have wayland.

To start a QT application using wayland you can check this service:

root@apalis-imx8:~# cat /lib/systemd/system/wayland-app-launch.service
[Unit]
Description=Start a wayland application
After=weston@root.service
Requires=weston@root.service

[Service]
Restart=on-failure
Type=forking
Environment="QT_QPA_PLATFORM=wayland-egl"
ExecStart=/usr/bin/wayland-app-launch.sh
RestartSec=1

[Install]
WantedBy=multi-user.target

It is recommended that for new developments you use the newest version of our BSP that’s available to ensure the best upgrade path for the future.

Best regards,
Rafael Beims

Hi @rafael.tx

Well, I use a bit old BSP version…

# cat /etc/issue
.---O---.                                           
|       |                  .-.           o o        
|   |   |-----.-----.-----.| |   .----..-----.-----.
|       |     | __  |  ---'| '--.|  .-'|     |     |
|   |   |  |  |     |---  ||  --'|  |  |  '  | | | |
'---'---'--'--'--.  |-----''----''--'  '-----'-'-'-'
                -'  |
                '---'

The Angstrom Distribution \n \l

Angstrom v2017.12 - Kernel 

Apalis-iMX6_LXDE-Image 2.8b6 20200228

What are the latest supported versions of the BSP and Qt for the Apalis-iMX6?

P.S.: Does “b” inside “2.8b6” mean beta?

Hi @vmetodiev

On Toradex’s BSP 5, Qt 5 is available.

Best regards,

With a lot of help from the Qt Community, I am posting the solution below.

xserver.service

[Unit]
Description=X Server
Conflicts=getty@tty1.service plymouth-quit.service
After=systemd-user-sessions.service getty@tty1.service plymouth-quit.service

[Service]
ExecStart=/usr/bin/X
ExecStop=/usr/bin/killall -9 X
Restart=always
IgnoreSIGPIPE=no

[Install]
Alias=display-manager.service

qtapp.service

[Unit]
Description=QT Application
Requires=xserver.service
After=xserver.service

[Service]
Type=simple
Environment="DISPLAY=:0"
ExecStart=/opt/TestQtApp
ExecStop=/bin/bash -c 'pkill TestQtApp'
Restart=always
IgnoreSIGPIPE=no

[Install]
WantedBy=multi-user.target

Hi @vmetodiev, that’s very good! Thank you for sharing back your solution.

P.S.: Does “b” inside “2.8b6” mean beta ?

No, it means build. This is the sixth stable build of the 2.8 series.
This versioning scheme was superceded by the one listed here.

Apart from that, please keep in mind that the 2.8 series is not recommended for new projects and will probably not get any updates anymore.

@rafael.tx

Alright, will take this into consideration.
Thank you as well for the provided information, Rafael!