X server closes automatically when launched using systemd

Hi! I am using a custom Linux distribution based on tdx-reference-multimedia-image with X11, which includes a Python desktop app based on GTK. I have made the following configurations for the app to start automatically at boot using systemd (according to this Toradex page):

x11-magusdl-init file, placed in /usr/bin:

#!/bin/sh

set -e

cd /home/root/magusdl
QTAPP="My app"
QTAPPSTART="/usr/bin/python3 main.py"

case "$1" in
  start)
        echo "Starting ${QTAPP}"
	    Xorg -nocursor &
        export DISPLAY=:0
	    eval $QTAPPSTART &
	    xfwm4
	    ;;
  stop)
        echo "Stopping ${QTAPP}"
                killall Xorg
        ;;
  restart)
        $0 stop
        $0 start
        ;;
  *)
        echo "usage: $0 { start | stop | restart }" >&2
        exit 1
        ;;
esac

exit 0

magusdl.service file, placed in /etc/systemd/system:

[Unit]
Description=Start X and my application

[Service]
Type=forking
ExecStart=/usr/bin/x11-magusdl-init start
ExecStop=/usr/bin/x11-magusdl-init stop

[Install]
WantedBy=graphical.target

In my case the X server does not start automatically (see this post) and I need to start it using the script. The application starts at boot as expected, but the problem is that the X server closes approx. 1 min after every boot (and the window manager and my app with it as well). I also get this behavior when starting the service manually, using the command systemctl start x11-magusdl.service.

However, if I run from the terminal the script located in /usr/bin, using the command ./x11-magusdl-init start, then the X server never closes.

This is the systemctl status output when the service is running (before it closes after approx. 1 min.):

systemctl status x11-magusdl.service
● x11-magusdl.service - Start X and my application
     Loaded: loaded (/lib/systemd/system/x11-magusdl.service; enabled; vendor preset: enabled)
     Active: activating (start) since Fri 2021-03-05 21:38:43 UTC; 1min 28s ago
Cntrl PID: 178 (x11-magusdl-ini)
     CGroup: /system.slice/x11-magusdl.service
             ├─178 /bin/sh /usr/bin/x11-magusdl-init start
             ├─184 Xorg -nocursor
             ├─185 /bin/sh /usr/bin/x11-magusdl-init start
             ├─186 /usr/bin/python3 main.py
             ├─188 xfwm4
             ├─410 dbus-launch --autolaunch f0d623ffec4440c5a7ef1c1303cbd4d4 --binary-syntax --close-stderr
             ├─411 /usr/bin/dbus-daemon --syslog-only --fork --print-pid 5 --print-address 7 --session
             ├─413 /usr/libexec/at-spi-bus-launcher
             ├─417 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3
             ├─430 /usr/lib/xfce4/xfconf/xfconfd
             └─439 /usr/libexec/at-spi2-registryd --use-gnome-session

Mar 05 21:38:47 colibri-imx6ull x11-magusdl-init[184]: Markers: (--) probed, (**) from config file, (==) default setting,
Mar 05 21:38:47 colibri-imx6ull x11-magusdl-init[184]:         (++) from command line, (!!) notice, (II) informational,
Mar 05 21:38:47 colibri-imx6ull x11-magusdl-init[184]:         (WW) warning, (EE) error, (NI) not implemented, (??) unknown.
Mar 05 21:38:47 colibri-imx6ull x11-magusdl-init[184]: (==) Log file: "/var/log/Xorg.0.log", Time: Fri Mar  5 21:38:47 2021
Mar 05 21:38:47 colibri-imx6ull x11-magusdl-init[184]: (==) Using config file: "/etc/X11/xorg.conf"
Mar 05 21:38:47 colibri-imx6ull x11-magusdl-init[184]: (==) Using system config directory "/usr/share/X11/xorg.conf.d"
Mar 05 21:39:03 colibri-imx6ull xfwm4[188]: Failed to connect to session manager: Failed to connect to the session manager: SESSION_MANAGER environment variable not defined
Mar 05 21:39:04 colibri-imx6ull org.a11y.Bus[417]: dbus-daemon[417]: Activating service name='org.a11y.atspi.Registry' requested by ':1.0' (uid=0 pid=188 comm="xfwm4 ")
Mar 05 21:39:04 colibri-imx6ull org.a11y.Bus[417]: dbus-daemon[417]: Successfully activated service 'org.a11y.atspi.Registry'
Mar 05 21:39:04 colibri-imx6ull org.a11y.Bus[439]: SpiRegistry daemon is running with well-known name - org.a11y.atspi.Registry

And this is the status after it closes:

 systemctl status x11-magusdl.service
● x11-magusdl.service - Start X and my application
     Loaded: loaded (/lib/systemd/system/x11-magusdl.service; enabled; vendor preset: enabled)
     Active: failed (Result: timeout) since Fri 2021-03-05 21:40:12 UTC; 4s ago
    Process: 178 ExecStart=/usr/bin/x11-magusdl-init start (code=killed, signal=TERM)

Mar 05 21:39:03 colibri-imx6ull xfwm4[188]: Failed to connect to session manager: Failed to connect to the session manager: SESSION_MANAGER environment variable not defined
Mar 05 21:39:04 colibri-imx6ull org.a11y.Bus[417]: dbus-daemon[417]: Activating service name='org.a11y.atspi.Registry' requested by ':1.0' (uid=0 pid=188 comm="xfwm4 ")
Mar 05 21:39:04 colibri-imx6ull org.a11y.Bus[417]: dbus-daemon[417]: Successfully activated service 'org.a11y.atspi.Registry'
Mar 05 21:39:04 colibri-imx6ull org.a11y.Bus[439]: SpiRegistry daemon is running with well-known name - org.a11y.atspi.Registry
Mar 05 21:40:12 colibri-imx6ull systemd[1]: x11-magusdl.service: start operation timed out. Terminating.
Mar 05 21:40:12 colibri-imx6ull xfwm4[188]: xfwm4: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.
Mar 05 21:40:12 colibri-imx6ull xfconfd[430]: Name org.xfce.Xfconf lost on the message dbus, exiting.
Mar 05 21:40:12 colibri-imx6ull x11-magusdl-init[184]: (II) Server terminated successfully (0). Closing log file.
Mar 05 21:40:12 colibri-imx6ull systemd[1]: x11-magusdl.service: Failed with result 'timeout'.
Mar 05 21:40:12 colibri-imx6ull systemd[1]: Failed to start Start X and my application.

It seems that when starting the app with systemd, the X server closes after approx. 1 min, due to some timeout (?). Does anybody have an idea how to resolve this problem?

It seems it was a service configuration problem. I’ve found a solution by adding an infinity start timeout:
[Unit]
Description=Start X and my application

[Service]
Type=forking
ExecStart=/usr/bin/x11-magusdl-init start
ExecStop=/usr/bin/x11-magusdl-init stop
TimeoutStartSec=infinity

[Install]
WantedBy=graphical.target

Now the service does not timeout, but its status is Activating. I don’t know if this is the best solution; I think a better solution would be one when the service status is Active.

Does anybody have a better suggestion for this?

HI @CristianM

Perfect that you found the solution. Thanks for the feedback.

Best regards,
Jaski