How to write systemed .service file to automatically start Qt application?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to write systemed .service file to automatically start Qt application?

8,908 Views
Takashi_Kashiwagi
Senior Contributor I

Hi Community.

I write .service as below but the Qt application does not start. 

[Unit]
Description=Test Qt Application
After=multi-user.target local-fs.target weston.service

[Service]
User=root
Restart=no
Type=simple
EnvironmentFile=/opt/test/root_env
ExecStart=/opt/test/app.sh
StandardOutput=journal+console

[Install]
WantedBy=multi-user.target weston.service

I want to start the QtApplication after weston.service starts. Is there anything wrong with it?

(When I checked dmesg, the service started before the western compositer.)

When I enter "systemctl start" after login, it will start normally.

/opt/test/app.sh is as follows.

#!/bin/sh
/opt/test/qtapp
exit 0

Qt Application log is as follows. "app.sh" is work. But QtAppliction is not work. 

QML debugging is enabled. Only use this in a safe environment.
Failed to create wl_display (No s
uch file or directory)
qt.qpa.plugin: Could not load the
Qt platform plugin "wayland" in "" even though it was found. because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: minimal, offscreen, vnc, wayland-egl, wayland.

Best Regards,

T.Kashiwagi

10 Replies

8,439 Views
f12358
Contributor I

Hi for all!

I solved the problem with autorun Qt app on iMX8M with xwayland.

A lot of thanks to Takashi Kashiwagi for his experiments.

My way to resolve the problem was:

I have my app (/opt/app)

I made an autorun.sh script (/opt/autorun.sh)

#!/bin/sh

sleep 5
/opt/app
exit 0

I made environment file (/opt/root_env)

LANG=C.UTF-8
EDITOR=vi
XDG_SESSION_ID=c2
HUSHLOGIN=FALSE
USER=root
PWD=/home/root
HOME=/home/root
XDG_SESSION_TYPE=tty
SHELL=/bin/sh
TERM=xterm
XDG_SESSION_CLASS=user
SHLVL=1
QT_QPA_PLATFORM=wayland
LOGNAME=root
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus
XDG_RUNTIME_DIR=/run/user/0
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
PS1=\u@\h:\w\$
_=/usr/bin/env

I made service file (/etc/systemd/system/autorun.service)

[Unit]
Description=Test Qt Application
After=multi-user.target local-fs.target weston.service

[Service]
User=root
Restart=no
Type=simple
EnvironmentFile=/opt/root_env
ExecStart=/opt/autorun.sh
StandardOutput=console

[Install]
WantedBy=multi-user.target weston.service

And at the end of all I edit rc.local file (/etc/rc.local)

#!/bin/sh -e

#

systemctl enable autorun.service

exit 0

The boot after this nothing happen, because autorun.service was not active during the boot, but the second time and others were succesfull!

8,439 Views
coindu
Contributor IV

you should add two lines in your app.sh

export XDG_RUNTIME_DIR=/run/user/0export QT_QPA_PLATFORM=wayland
0 Kudos

8,439 Views
Takashi_Kashiwagi
Senior Contributor I

Hi coin-san

Thank you for reply.

> you should add two lines in your app.sh

I have already done that setting by "EnvironmentFile"

The env file is as follows.

LANG=C.UTF-8
EDITOR=vi
XDG_SESSION_ID=c2
HUSHLOGIN=FALSE
USER=root
PWD=/home/root
HOME=/home/root
XDG_SESSION_TYPE=tty
TSLIB_TSDEVICE=/dev/input/touchscreen0
MAIL=/var/spool/mail/root
SHELL=/bin/sh
TERM=xterm
XDG_SESSION_CLASS=user
SHLVL=1
QT_QPA_PLATFORM=wayland
LOGNAME=root
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus
XDG_RUNTIME_DIR=/run/user/0
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
PS1=\u@\h:\w\$
_=/usr/bin/env

Best Regards,

T.Kashiwagi

0 Kudos

8,439 Views
b36401
NXP Employee
NXP Employee

Please try to add something like "export DISPLAY=:0" into the script running the application.

0 Kudos

8,439 Views
Takashi_Kashiwagi
Senior Contributor I

Hi Victor-san

Thank you for reply.

> Please try to add something like "export DISPLAY=:0" into the script running the application.

This didn't work either. 

I can work around the problem by adding the following to the [Service] section of the .service file, but I think that this is not good.

[Service]
Restart=on-failure
RestartSec=2s
Type=simple

according to [PATCH weston] doc/systemd: system service example , If I make changes to weston.ini, I think weston will send sd_notify to systemd , but it does not work. Are the settings incorrect?

$ weston --modules=systemd-notify.so

or in weston.ini:

[core]
modules=systemd-notify.so

The plugin implements the systemd service notification protocol, watchdog protocol, and also allows socket activation and configuring listening sockets via systemd.

Best Regards,

T.Kashiwagi

0 Kudos

8,439 Views
Takashi_Kashiwagi
Senior Contributor I

Hi Community.

> I will change the setting to Type = oneshot and check.

This did not work. Qtapp.service did not start.

I wrote [Unit] section as follows. The service startup order is now what I intended.

[Unit]
Description=Test Qt Application
After=multi-user.target local-fs.target weston.service systemd-user-sessions.service
RequiresMountsFor=/sys/class/backlight/backlight/

[ OK ] Started Weston Wayland Compositor (on tty7).
Atheros 8031 ethernet 30be0000.ethernet-1:00: attached PHY driver [Atheros 8031 ethernet] (mii_bus:phy_addr=30be0000.ethernet-1:00, irq=POLL)
IPv6: ADDRCONF(NETDEV_UP): eth0: link is not ready
[ OK ] Started Network Manager Script Dispatcher Service.
[ OK ] Created slice User Slice of UID 0.
Starting User Runtime Directory /run/user/0...
Starting Authorization Manager...
[ OK ] Reached target Multi-User System.
Starting Update UTMP about System Runlevel Changes...
Starting Test Qt Application...

But it will fail without sleep.

Best Regards,

T.Kashiwagi

0 Kudos

8,439 Views
turker
Contributor IV

Does your application work as expected when you launch it manually? If that is the case can you try modifying your service file as shown below and try again?

[Unit]

...

After=weston.service

Requires=weston.service

[Service]

...

[Install]

WantedBy=multi-user.target

0 Kudos

8,439 Views
Takashi_Kashiwagi
Senior Contributor I

Hi Tahsin-san.

Thank you for replay.

> Does your application work as expected when you launch it manually?

Yes, it dose.

> If that is the case can you try modifying your service file as shown below and try again?

I tried it. But qt application did not start. 

I checked the startup order in "systemctl list-dependencies", but it seems that it was started before weston.service.

|-systemd-update-utmp-runlevel.service
   |-systemd-user-sessions.service
   |-qtapp.service
   |-weston.service
   |-basic.target

Best Regards,

T.Kashiwagi

0 Kudos

8,439 Views
turker
Contributor IV

Hi Takashi-san,

Maybe some environment variables required by weston are not set when launching your application on start up. I suggest you to put something like env > /dev/console or systemd-cat env inside /opt/test/app.sh and check the output on system startup. Then run the env command manually and compare the outputs to see if there are any differences.

Regards,

Tahsin

0 Kudos

8,439 Views
Takashi_Kashiwagi
Senior Contributor I

Hi Tahsin-san.

 

Thank you for replay.

Then run the env command manually and compare the outputs to see if there are any differences.

I copied the execution result of printenv to "/opt/test/root_env" as it was, but the Qt application did not start.

But, Qt Application started when I added "sleep 2s" before "/opt/test/qtapp" in the .sh file.

So, I checked [Service] section in "weston.service".

There is no "Type=".

I think that systemd process the service as "Type = simple" if Type is not specified.

I will change the setting to Type = oneshot and check.

Best Regards,

T.Kashiwagi

0 Kudos