QProcess::StartDetached, but the child process closes when the parent exits
-
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.
-
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.
@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());
-
@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? -
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?@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);
-
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... -
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...@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?
-
@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 variableQProcess
, or did you just check the heap case and not the stack case? -
@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?
@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 -
@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@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... :)
-
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. -
@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 variableQProcess
, or did you just check the heap case and not the stack case?@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. -
@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. -
@JoeCFD
My thought is thatQProcess::start()
does require theQProcess
instance to stay in scope as long as the process is running, butQProcess::startDetached()
should presumably arrange that it doesn't matter if it goes out of scope.