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. QProcess::StartDetached, but the child process closes when the parent exits
Forum Updated to NodeBB v4.3 + New Features

QProcess::StartDetached, but the child process closes when the parent exits

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 2.4k Views
  • 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.
  • M Offline
    M Offline
    mewster
    wrote on last edited by
    #1

    I have a QProcess

    QProcess playerProcess;
    playerProcess.setProgram("/bin/sh");
    playerProcess.setArguments(QStringList{"script.sh", "param1 param2 param3});
    

    that starts detached through

    if (playerProcess.startDetached())
    {
    	sleep(10);
    	QCoreApplication::quit();
    }
    

    but when the main application quits, the script gets closed too (I put the sleep to verify that the script get executed while the main application is still running).

    I checked through ps -ef that the spawned process doesn't have the main application's parent pid (it has 1).

    I'm using QT 5.12.3

    Any suggestion?

    Thanks.

    KroMignonK 1 Reply Last reply
    0
    • M mewster

      I have a QProcess

      QProcess playerProcess;
      playerProcess.setProgram("/bin/sh");
      playerProcess.setArguments(QStringList{"script.sh", "param1 param2 param3});
      

      that starts detached through

      if (playerProcess.startDetached())
      {
      	sleep(10);
      	QCoreApplication::quit();
      }
      

      but when the main application quits, the script gets closed too (I put the sleep to verify that the script get executed while the main application is still running).

      I checked through ps -ef that the spawned process doesn't have the main application's parent pid (it has 1).

      I'm using QT 5.12.3

      Any suggestion?

      Thanks.

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

      @mewster said in QProcess::StartDetached, but the child process closes when the parent exits:

      Any suggestion?

      From documentation:

      The called process inherits the console window of the calling process. To suppress console output, redirect standard/error output to QProcess::nullDevice().

      Maybe you have to redirect standard I/O to null:

      playerProcess.setStandardErrorFile(QProcess::nullDevice());
      playerProcess.setStandardInputFile(QProcess::nullDevice());
      playerProcess.setStandardOutputFile(QProcess::nullDevice());
      

      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
      • M Offline
        M Offline
        mewster
        wrote on last edited by
        #3

        I don't think it has anything to do with the console output....

        KroMignonK 1 Reply Last reply
        0
        • M mewster

          I don't think it has anything to do with the console output....

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

          @mewster said in QProcess::StartDetached, but the child process closes when the parent exits:

          I don't think it has anything to do with the console output....

          But it is the only link between the running process and forked process.
          Have you tried it out?

          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
          • M Offline
            M Offline
            mewster
            wrote on last edited by
            #5

            Yep, it still closes.
            I'm thinking it's something about the process groups; is it possible to set a different group to a process through QT?

            KroMignonK 1 Reply Last reply
            0
            • M mewster

              Yep, it still closes.
              I'm thinking it's something about the process groups; is it possible to set a different group to a process through QT?

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

              @mewster said in QProcess::StartDetached, but the child process closes when the parent exits:

              I'm thinking it's something about the process groups; is it possible to set a different group to a process through QT?

              This don't made sense to me. QCoreApplication::quit() stops application and not any forked process.
              Perhaps you have to setup working directory?

              playerProcess.setWorkingDiretory(pathOfScript);
              

              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
              • M Offline
                M Offline
                mewster
                wrote on last edited by mewster
                #7

                Found a dirty fix: changing the group id on the child sh script itself lets it live after the parent's exit.
                Maybe there is some kind of indirect reference outside of QT's process management that keeps the two processes entwined...

                JoeCFDJ JonBJ 2 Replies Last reply
                1
                • M mewster

                  Found a dirty fix: changing the group id on the child sh script itself lets it live after the parent's exit.
                  Maybe there is some kind of indirect reference outside of QT's process management that keeps the two processes entwined...

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by JoeCFD
                  #8

                  @mewster Try to use pointer without destroying it to see it helps.

                  auto player_process = new QProcess;
                  player_process->setProgram("/bin/sh");
                  player_process->setArguments(QStringList{"script.sh", "param1 param2 param3});

                  I use pointer in my code and the program still runs after qt app exits.

                  JonBJ 1 Reply Last reply
                  0
                  • M mewster

                    Found a dirty fix: changing the group id on the child sh script itself lets it live after the parent's exit.
                    Maybe there is some kind of indirect reference outside of QT's process management that keeps the two processes entwined...

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @mewster
                    I had typed in a whole answer, but what you have just said changes everything!

                    It certainly should not be anything "Qt" or "QProcess" which is at issue; I would expect it to behave the same from any parent program.

                    I'm intrigued by your "changing the group id on the child sh script itself lets it live after the parent's exit". I don't know about that. Nothing to do with the terminal group then?

                    M 1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      @mewster Try to use pointer without destroying it to see it helps.

                      auto player_process = new QProcess;
                      player_process->setProgram("/bin/sh");
                      player_process->setArguments(QStringList{"script.sh", "param1 param2 param3});

                      I use pointer in my code and the program still runs after qt app exits.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @JoeCFD said in QProcess::StartDetached, but the child process closes when the parent exits:

                      I use pointer in my code and the program still runs after qt app exits.

                      To me this change makes no sense, because on program exit the new QProcess is still destroyed.

                      Did you actually check that the child, whatever you are using, did get terminated with a stack variable QProcess and did not get terminated with a heap variable QProcess, or did you just check the heap case and not the stack case?

                      JoeCFDJ 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @mewster
                        I had typed in a whole answer, but what you have just said changes everything!

                        It certainly should not be anything "Qt" or "QProcess" which is at issue; I would expect it to behave the same from any parent program.

                        I'm intrigued by your "changing the group id on the child sh script itself lets it live after the parent's exit". I don't know about that. Nothing to do with the terminal group then?

                        M Offline
                        M Offline
                        mewster
                        wrote on last edited by
                        #11

                        @JonB Unfortunately I'm not that expert on the system's process management, I don't even know what is the terminal group...
                        I tried a possible solution I found after searching different keywords on Stackoverflow, but at this point I'm positive it has nothing to do with QT, I too would have expected a standard behaviour and it would have been a pretty solid bug (that should have been already found) otherwise

                        JonBJ 1 Reply Last reply
                        1
                        • M mewster

                          @JonB Unfortunately I'm not that expert on the system's process management, I don't even know what is the terminal group...
                          I tried a possible solution I found after searching different keywords on Stackoverflow, but at this point I'm positive it has nothing to do with QT, I too would have expected a standard behaviour and it would have been a pretty solid bug (that should have been already found) otherwise

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

                          @mewster
                          Understood :) I just wanted to know why the solution! :)

                          You ought just briefly try what @JoeCFD has said to see if it fixes, instead of your "group id on the child sh script". I have said I would find it "surprising" if what he suggests fixes your situation (I would have thought it was to do with whatever your script does), but I could be wrong, and I'd like to hear if it magically fixes for you!? In which case, I would raise a separate question about it in this forum... :)

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mewster
                            wrote on last edited by
                            #13

                            Ok, I found the problem.

                            While the QT application was working correctly, it was launched through a systemd service.
                            Even if the application would start a detached process, the underlying system would kill it anyway when the parent process would exit (keyword: "KillMode=process").
                            Changing the group id of the detached process would effectively detach it even from QT's parent, systemd.

                            1 Reply Last reply
                            2
                            • JonBJ JonB

                              @JoeCFD said in QProcess::StartDetached, but the child process closes when the parent exits:

                              I use pointer in my code and the program still runs after qt app exits.

                              To me this change makes no sense, because on program exit the new QProcess is still destroyed.

                              Did you actually check that the child, whatever you are using, did get terminated with a stack variable QProcess and did not get terminated with a heap variable QProcess, or did you just check the heap case and not the stack case?

                              JoeCFDJ Offline
                              JoeCFDJ Offline
                              JoeCFD
                              wrote on last edited by JoeCFD
                              #14

                              @JonB my process call start() to launch a gstreamer pipeline which has to be killed manually when my app exits.
                              I see in this case it is a bit different. Destroying the process will not kill the pipeline. Instead CTRL+C is needed to stop the pipeline.

                              JonBJ 1 Reply Last reply
                              0
                              • JoeCFDJ JoeCFD

                                @JonB my process call start() to launch a gstreamer pipeline which has to be killed manually when my app exits.
                                I see in this case it is a bit different. Destroying the process will not kill the pipeline. Instead CTRL+C is needed to stop the pipeline.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #15

                                @JoeCFD
                                My thought is that QProcess::start() does require the QProcess instance to stay in scope as long as the process is running, but QProcess::startDetached() should presumably arrange that it doesn't matter if it goes out of scope.

                                JoeCFDJ 1 Reply Last reply
                                1
                                • JonBJ JonB

                                  @JoeCFD
                                  My thought is that QProcess::start() does require the QProcess instance to stay in scope as long as the process is running, but QProcess::startDetached() should presumably arrange that it doesn't matter if it goes out of scope.

                                  JoeCFDJ Offline
                                  JoeCFDJ Offline
                                  JoeCFD
                                  wrote on last edited by
                                  #16

                                  @JonB That is true.

                                  1 Reply Last reply
                                  0

                                  • Login

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