Process and child process
-
I have a process that launches another application, both applications are written by myself. I've run the child process in Qt Creator and it functions as I would expect and stays running until I terminate it.
When I run the main process it launches the child with:
QString strFullPath(clsDebugService::strGetUserFolder(strApp)); int64PID = 0; qdbg() << "Checking for PID for: " << strFullPath; if ( blnGetPID(strFullPath, int64PID) == true ) { //Process already running, no action required } else { int intLastSep = strFullPath.lastIndexOf(QDir::separator()); if ( intLastSep > 0 ) { QString strName = strFullPath.mid(intLastSep + 1) ,strPath = strFullPath.mid(0, intLastSep + 1); qdbg() << "Launching: " << strFullPath; mspobjProcess->startDetached(strFullPath, slstArgs, strPath, &int64PID); } } if ( int64PID > 0 ) { qdbg() << "Process: " << strFullPath << ", is running PID: " << QString::number(int64PID); return true; } return false;This is the body of my launching function. I can see that the child process is successfully launched and the PID is returned, however almost instantly the child process terminates and the PID is no longer listed. I'm struggling to determine why? All the processes create log files and log output.
Is there anything that could explain why is terminates? The host computer is an iMAC:
macOS Catalina Version 10.15.7 iMac (Retina 5K, 27-inch, Late 2015) Processor 4 GHz Quad-Core Intel Core i7 Memory 16 GB 1867 MHz DDR3 Graphics AMD Radeon R9 M395X 4GBQt:
Qt Creator 4.13.2 Based on Qt 5.15.1 (Clang 11.0 (Apple), 64 bit) Built on Oct 1 2020 01:16:45 From revision 2ee1af2032@SPlatten said in Process and child process:
Is there anything that could explain why is terminates?
Please first add proper error handling to your code (QProcess provides methods for that).
-
@SPlatten said in Process and child process:
Is there anything that could explain why is terminates?
Please first add proper error handling to your code (QProcess provides methods for that).
-
@jsulm , any suggestions on how? As far as I can see QProcess is launching the application and a valid PID is returned, there is also output in the applications log file so I know it launches.
@SPlatten Then read from https://doc.qt.io/qt-5/qprocess.html#readAllStandardError to see whether the application started by QProcess prints any warnings/errors.
And you still should check https://doc.qt.io/qt-5/qprocess.html#error and/or connect a slot to https://doc.qt.io/qt-5/qprocess.html#errorOccurred as an error can occur after starting the process. -
@SPlatten Then read from https://doc.qt.io/qt-5/qprocess.html#readAllStandardError to see whether the application started by QProcess prints any warnings/errors.
And you still should check https://doc.qt.io/qt-5/qprocess.html#error and/or connect a slot to https://doc.qt.io/qt-5/qprocess.html#errorOccurred as an error can occur after starting the process. -
@jsulm , I just added:
qdbg() << mspobjProcess->readAllStandardError(); qdbg() << mspobjProcess->readAllStandardOutput();And the output in the log file was:
QIODevice::read (QProcess): device not open QIODevice::read (QProcess): device not open -
@jsulm , I just added:
qdbg() << mspobjProcess->readAllStandardError(); qdbg() << mspobjProcess->readAllStandardOutput();And the output in the log file was:
QIODevice::read (QProcess): device not open QIODevice::read (QProcess): device not open@SPlatten
I am not 100% sure that @jsulm has noticed you are usingstartDetached(), and I'm not 100% sure whether for that you must instead usesetStandardErrorFile()etc. instead, as per https://doc.qt.io/qt-5/qprocess.html#startDetached ?EDIT Hold on! The overload you are using, https://doc.qt.io/qt-5/qprocess.html#startDetached-1, is
static. So you're not going to have amspobjProcessinstance? For more control over it use the https://doc.qt.io/qt-5/qprocess.html#startDetached overload I referred to earlier. -
@SPlatten
I am not 100% sure that @jsulm has noticed you are usingstartDetached(), and I'm not 100% sure whether for that you must instead usesetStandardErrorFile()etc. instead, as per https://doc.qt.io/qt-5/qprocess.html#startDetached ?EDIT Hold on! The overload you are using, https://doc.qt.io/qt-5/qprocess.html#startDetached-1, is
static. So you're not going to have amspobjProcessinstance? For more control over it use the https://doc.qt.io/qt-5/qprocess.html#startDetached overload I referred to earlier. -
-
@jsulm To be honest, probably because the example I came across for QProcess was using it. What I want is to launch the child process and allow the launching process to carry on without being held up.
-
@jsulm To be honest, probably because the example I came across for QProcess was using it. What I want is to launch the child process and allow the launching process to carry on without being held up.
@SPlatten said in Process and child process:
What I want is to launch the child process and allow the launching process to carry on without being held up.
That happens without
startDetached(),QProcessruns asynchronously. You only needstartDetachedbasically if you want to sub-process not to e.g. get terminated when your process exits. -
@SPlatten said in Process and child process:
What I want is to launch the child process and allow the launching process to carry on without being held up.
That happens without
startDetached(),QProcessruns asynchronously. You only needstartDetachedbasically if you want to sub-process not to e.g. get terminated when your process exits. -
-
@jsulm , I get the PID so I can verify its actually running since neither start or startDetached return anything.
-
@jsulm , I get the PID so I can verify its actually running since neither start or startDetached return anything.
@SPlatten said in Process and child process:
@jsulm , I get the PID so I can verify its actually running since neither start or startDetached return anything.
well, Process has the started and stateChanged signal for that.
https://doc.qt.io/qt-5/qprocess.html#startedyou should actually listen to the stateChanged signal, to see if it changes from Running to NotRunning
-
@jsulm , I get the PID so I can verify its actually running since neither start or startDetached return anything.
@SPlatten
You have other things you can look at instead, likeQProcess::stateChangedsignal for clues.While you get it working, you may find
start()is easier to work with thanstartDetached().EDIT Sigh, looks like 3 of us are all trying to answer :)
If it's not secret, you might like to share with us the full command you are running, including the arguments, in case we can spot anything for you....
-
3 People 3 times the same thought
Up Top🙌
-
@SPlatten
You have other things you can look at instead, likeQProcess::stateChangedsignal for clues.While you get it working, you may find
start()is easier to work with thanstartDetached().EDIT Sigh, looks like 3 of us are all trying to answer :)
If it's not secret, you might like to share with us the full command you are running, including the arguments, in case we can spot anything for you....
@JonB I just tried start and it didn't work, I got errors in the Application Output:
QObject: Cannot create children for a parent that is in a different thread. (Parent is QProcess(0x101e0b420), parent's thread is QThread(0x101e0b880), current thread is clsThread(0x122c1bcc0) -
@JonB I just tried start and it didn't work, I got errors in the Application Output:
QObject: Cannot create children for a parent that is in a different thread. (Parent is QProcess(0x101e0b420), parent's thread is QThread(0x101e0b880), current thread is clsThread(0x122c1bcc0) -
@SPlatten said in Process and child process:
Why does startDetached take a qint64 for the PID as the last parameter but the QProcess function pid returns not a qint64 but a Q_PID?
Why are these different types?Don't use
Q_PIDfor this (Windows). Useqint64 QProcess::processId() const.