I'm using Qt, QProcess to launch a process, but there is presently no way to use QProcess to check existing running processes.
I need a way of achieving this on multiple platforms. I have an application name that I want to look up, if its running I want to get it's PID. If it isn't running I will create an instance of it, this last bit I can do.
I'm working on a Mac and so far I've done the following:
pid_t pid = fork();
if ( pid > 0 ) {
int intStatus;
while ( (pid = waitpid(-1, &intStatus, WNOHANG)) == 0) {
system("ps -A");
qDebug() << "Still waiting!";
sleep(20);
}
qDebug() << "Exit Status %d" << WEXITSTATUS(intStatus);
}
The above executes the command and the output is dumped in the console, I need to capture this so I can process it.
Also looking for a way to achieve this on Windows platforms.
On Posix systems, you could capture the output of ps
by using popen
and then parse it. The popen
part looks like this:
#include <iostream>
#include <cstdio>
int main()
{
FILE *f = popen ("ps -A", "r");
char s [1024];
while (fgets (s, sizeof (s), f))
std::cout << s;
pclose (f);
}