PDA

View Full Version : QProcess and Pipes



KaptainKarl
9th April 2007, 22:59
I'm creating a server monitor and want to get a process table listing of certain processes. My first, and probably incorrect, thought was to create a QProcess and call the UNIX 'ps' command and then parse the standard output.

The following code works fine:
NOTE: The cvtLogger function appends the QString argument to a QTextEdit.
QString sCmd = "ps"; QStringList slArgs; slArgs.append("aux"); QProcess *pServerCheck = new QProcess; pServerCheck->setReadChannelMode(QProcess::MergedChannels); pServerCheck->start(sCmd, slArgs); if (!pServerCheck->waitForFinished()) {
cntlLogger(pServerCheck->errorString()); } else { cntlLogger(pServerCheck->readAll()); } delete pServerCheck;Now, if I try to use a pipe to grep to get just the process I want, it doesn't work.
QString sCmd = "ps";
QStringList slArgs;
slArgs.append("aux");
slArgs.append("| grep SMRTemd");
QProcess *pServerCheck = new QProcess;
pServerCheck->setReadChannelMode(QProcess::MergedChannels);
pServerCheck->start(sCmd, slArgs);
if (!pServerCheck->waitForFinished())
{
cntlLogger(pServerCheck->errorString());
}
else
{
cntlLogger("EMD PID: " + pServerCheck->readAll());
}
delete pServerCheck;The output comes through the "readAll" branch is:
EMD PID: ERROR: Garbage option.I also tried putting "ps aux | grep SMRTemd" into a single QString and passing it to QProcess that way. It returned the same error.

Is there an easier way to do this that I'm not thinking of?

Thanks,

Karl

jacek
9th April 2007, 23:11
You are invoking ps with two parameters: "aux" and "| grep SMRTemd". Just as if you wrote:
$ ps aux "| grep SMRTemd"

If you want to make | work, start a shell:
QString sCmd( "/bin/sh" );
QStringList slArgs;
slArgs << "-c" << "ps aux | grep SMRTemd";