readProcessOutput() - mac


#1

I’m getting an empty string when trying to do

childProcess.start("ps");
String output = childProcess.readAllProcessOutput();

It’s also commented in the unit-test

class ChildProcessTests  : public UnitTest
{
public:
    ChildProcessTests() : UnitTest ("ChildProcess") {}

    void runTest()
    {
        beginTest ("Child Processes");

      #if JUCE_WINDOWS || JUCE_MAC || JUCE_LINUX
        ChildProcess p;

       #if JUCE_WINDOWS
        expect (p.start ("tasklist"));
       #else
        expect (p.start ("ls /"));
       #endif

        //String output (p.readAllProcessOutput());
        //expect (output.isNotEmpty());
      #endif
    }
};

Maybe it’s because there’s no more output by the time the readProcessOutput gets called?


#2

Yeah, I had some problems with that, and never managed to figure out what was going wrong. I think it might indeed be that the process has already finished when it gets called.


#3

I ended up using this code, if anyone needs:

    FILE* pipe = popen(command.toUTF8().getAddress(), "r");
    
    if (!pipe) 
        return String::empty;
    
    char buffer[128];
    String result = String::empty;
    
    while(!feof(pipe)) 
    {
        if(fgets(buffer, 128, pipe) != NULL)
            result += String(buffer);
    }
    pclose(pipe);
    return result;