Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Starting QT app as a Systemd service
Qt 6.11 is out! See what's new in the release blog

Starting QT app as a Systemd service

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 6 Posters 11.2k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vmetodiev
    wrote on last edited by
    #5

    @Pl45m4, @KroMignon

    Well, here is a snippet from my main function:

    int main(int argc, char *argv[])
    {
         QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
         QGuiApplication app(argc, argv);
         QQmlApplicationEngine engine;
         ...
    }	
    

    I will read a bit more about QCore and QGui... now it is some kind of a Frankenstein :D

    @SGaist

    Yes, I always start the X server before attempting to launch the QT application.
    It also starts correctly as a systemd service.

    About "eglfs" - thanks for mentioning this, I will read about it as well and write another post. I have no idea what you are taking about right now.

    Thank you guys for your support and informative directions!

    JonBJ 1 Reply Last reply
    0
    • V vmetodiev

      @Pl45m4, @KroMignon

      Well, here is a snippet from my main function:

      int main(int argc, char *argv[])
      {
           QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      
           QGuiApplication app(argc, argv);
           QQmlApplicationEngine engine;
           ...
      }	
      

      I will read a bit more about QCore and QGui... now it is some kind of a Frankenstein :D

      @SGaist

      Yes, I always start the X server before attempting to launch the QT application.
      It also starts correctly as a systemd service.

      About "eglfs" - thanks for mentioning this, I will read about it as well and write another post. I have no idea what you are taking about right now.

      Thank you guys for your support and informative directions!

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #6

      @vmetodiev
      I am not an expert, but how can you have a QGuiApplication and/or a QQmlApplicationEngine if you're not going to have any UI or interaction?

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vmetodiev
        wrote on last edited by
        #7

        @JonB

        I agree! My goal is to make the GUI application start as a service with only the X server running beneath it, without LXDE or any other desktop environment.

        I am just struggling how to achieve it no additional overhead...

        KroMignonK 1 Reply Last reply
        0
        • V vmetodiev

          @JonB

          I agree! My goal is to make the GUI application start as a service with only the X server running beneath it, without LXDE or any other desktop environment.

          I am just struggling how to achieve it no additional overhead...

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #8

          @vmetodiev said in Starting QT app as a Systemd service:

          I agree! My goal is to make the GUI application start as a service with only the X server running beneath it, without LXDE or any other desktop environment.
          I am just struggling how to achieve it no additional overhead...

          So you want to start you Qt QML/QWidget application with SystemD and X-Server.
          Your error looks very similar to this ==> https://community.toradex.com/t/starting-qt-app-as-a-systemd-service/15954

          So I guess you have to change qtapp.service to setup QT_QPA_PLATFORM.
          Something like this:

          [Unit]
          Description=QT Application
          After=xserver.service
          
          [Service]
          Type=simple
          Environment="QT_QPA_PLATFORM=wayland-egl"
          ExecStart=/opt/TestQtApp
          ExecStop=/bin/bash -c 'pkill TestQtApp'
          Restart=always
          IgnoreSIGPIPE=no
          
          [Install]
          Alias=qtapp.service
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          0
          • V vmetodiev

            Hello QT Community,

            I need to start my QT application without XFCE, Gnome, KDE, or what-so-ever desktop environment. I have disabled the lxdm.service (on my Toradex board running Angstrom Linux) 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
            

            This 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.
            

            Merging the commands for starting the X server and the QT application within a single service behaves similarly - I see the X process running, but not the QT application.

            Logging into the Bash shell and executing the "ExecStart" commands manually launches the QT application correctly.

            May someone advise on this?

            Thank you in advance!

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by raven-worx
            #9

            @vmetodiev said in Starting QT app as a Systemd service:

            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.

            i think you are just missing the "DISPLAY=:0" env variable?

            try it manually by calling

            DISPLAY=:0 /opt/TestQtApp
            

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vmetodiev
              wrote on last edited by
              #10

              @KroMignon
              Your error looks very similar to this ==> https://community.toradex.com/t/starting-qt-app-as-a-systemd-service/15954
              -> Absolutely :D It was posted by me in the Toradex forum, than I switched here...

              Environment="QT_QPA_PLATFORM=wayland-egl"
              -> As far I understand, in this case the QT app will work with Wayland instead of the X server, correct?

              @raven-worx
              Well, I added it as an enviroment and achived a certain progress.

              root@apalis-imx6:~# cat /lib/systemd/system/qtapp.service
              [Unit]
              Description=QT Application
              After=xserver.service
              
              [Service]
              Type=simple
              Environment="DISPLAY=:0"
              ExecStart=/opt/TestQtApp
              ExecStop=/bin/bash -c 'pkill TestQtApp'
              Restart=always
              IgnoreSIGPIPE=no
              
              [Install]
              Alias=qtapp.service
              
              

              The service now starts when I manually invoke it by "systemctl start qtapp".
              Nevertheless, it won't start automatically after reboot. The journal log is empty. Maybe the "After=xserver.service" is not quite correct?

              raven-worxR 1 Reply Last reply
              0
              • V vmetodiev

                @KroMignon
                Your error looks very similar to this ==> https://community.toradex.com/t/starting-qt-app-as-a-systemd-service/15954
                -> Absolutely :D It was posted by me in the Toradex forum, than I switched here...

                Environment="QT_QPA_PLATFORM=wayland-egl"
                -> As far I understand, in this case the QT app will work with Wayland instead of the X server, correct?

                @raven-worx
                Well, I added it as an enviroment and achived a certain progress.

                root@apalis-imx6:~# cat /lib/systemd/system/qtapp.service
                [Unit]
                Description=QT Application
                After=xserver.service
                
                [Service]
                Type=simple
                Environment="DISPLAY=:0"
                ExecStart=/opt/TestQtApp
                ExecStop=/bin/bash -c 'pkill TestQtApp'
                Restart=always
                IgnoreSIGPIPE=no
                
                [Install]
                Alias=qtapp.service
                
                

                The service now starts when I manually invoke it by "systemctl start qtapp".
                Nevertheless, it won't start automatically after reboot. The journal log is empty. Maybe the "After=xserver.service" is not quite correct?

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by raven-worx
                #11

                @vmetodiev
                do you start the xserver service manually?
                if not you are also missing Require=xserver.service so it is ensured that its running when your qt app gets started

                also to start it after reboot, you have to call systemctl enable qtapp

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                1
                • V Offline
                  V Offline
                  vmetodiev
                  wrote on last edited by
                  #12

                  @raven-worx

                  Yes, the service is enabled. Well, I added "Requires=xserver.service", but still the qtapp.service would not start.

                  The xserver is fine upon reboot, visible inside "ps -auxf".

                  Invoking " systemctl start qtapp" start the Qt app...

                  KroMignonK 1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    Maybe this service example might help.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • V vmetodiev

                      @raven-worx

                      Yes, the service is enabled. Well, I added "Requires=xserver.service", but still the qtapp.service would not start.

                      The xserver is fine upon reboot, visible inside "ps -auxf".

                      Invoking " systemctl start qtapp" start the Qt app...

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by
                      #14

                      @vmetodiev said in Starting QT app as a Systemd service:

                      Invoking " systemctl start qtapp" start the Qt app...

                      I am far a way to be a systemd expert, so I may be wrong.
                      But I guess you have to add WantedBy=multi-user.target in the [Install] section to start the service automatically.

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      raven-worxR 1 Reply Last reply
                      1
                      • KroMignonK KroMignon

                        @vmetodiev said in Starting QT app as a Systemd service:

                        Invoking " systemctl start qtapp" start the Qt app...

                        I am far a way to be a systemd expert, so I may be wrong.
                        But I guess you have to add WantedBy=multi-user.target in the [Install] section to start the service automatically.

                        raven-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on last edited by
                        #15

                        @KroMignon said in Starting QT app as a Systemd service:

                        But I guess you have to add WantedBy=multi-user.target in the [Install] section to start the service automatically.

                        yep, thats true, good catch

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply
                        0
                        • V Offline
                          V Offline
                          vmetodiev
                          wrote on last edited by
                          #16

                          @SGaist @KroMignon
                          Yes, your suggestions helped. Now it works perfectly. Thank you so much for your support!!!

                          I am providing the service files 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
                          
                          1 Reply Last reply
                          1
                          • Y Yifan 0 referenced this topic on

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved