How to close the subroutine of the program started by qprocess?
-
@jiaming
You can't. Your Qt app can only do something to the A sub-process it started, not some B sub-process which A starts. Hopefully when A terminates B will terminate too, but you have no control over that. I assume you're usingQProcess::start()
and notQProcess::startDetached()
, FWIW. -
Then fix A so it properly shuts down B
-
@jiaming
As @Christian-Ehrlicher has said. And as I said earlier, what else would you like us to say? Just because you would like A to shut down B does not mean Qt app has any control over this, or that we can wave magic wand to make it so it does.... -
Try this
kill( m_process->pid(), SIGINT );
before m_process is closed.or
QString cmd = QString( "kill -9 %1" ).arg( m_process->pid() );
call this
exec( cmd.toStdString().c_str() );std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
from https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-po -
@JoeCFD How should this help killing a sub-sub-process? Apart from this the OP is on windows...
-
Then 'kill' should be available. But it still does not solve your problem. Fix your 'A' program to properly shut down it's spawned processes.
-
My program runs on Ubuntu
And so? As I & @Christian-Ehrlicher have said, you cannot use this to kill a "sub-sub-process". You don't know the PID of the process you want to kill.
Can you please take the time to address what @Christian-Ehrlicher wrote earlier:
Then fix A so it properly shuts down B
[Under Linux you could try using
pkill
to kill allB
processes, but I really wouldn't recommend it....] -
@jiaming said in How to close the subroutine of the program started by qprocess?:
I found that PPID of B is 1
You need PID if you want to kill it...
-
@jiaming You could try to get PID from B using PID from A, see https://stackoverflow.com/questions/17743879/how-to-get-child-process-from-parent-process
-
@jiaming said in How to close the subroutine of the program started by qprocess?:
I found that PPID of B is 1......
Which is why I said I do not think that sending a
SIGINT
to the parent (A
) will have any effect onB
...!A process ID value of 1 indicates that there is no parent process associated with the [...] process."
-
@jiaming kill( m_process->pid(), SIGINT ); <==this does what you need. You need somehow to find the pid of B.
std::string pid_str = exec( "pidof B" ); exec is defined in the post above and "pidof" is a linux command to find process id.
if( !pid_str.empty() ) {
int pid = std::stoi( pid_str );
kill( pid, SIGINT );
}