Results 1 to 2 of 2

Thread: QProcess and Pipes

  1. #1
    Join Date
    Feb 2006
    Location
    Jarrell, Texas, USA
    Posts
    70
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Question QProcess and Pipes

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess and Pipes

    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:
    Qt Code:
    1. QString sCmd( "/bin/sh" );
    2. QStringList slArgs;
    3. slArgs << "-c" << "ps aux | grep SMRTemd";
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to jacek for this useful post:

    KaptainKarl (10th April 2007)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.