ChildProcess: How to pipe commands bash-style

I’ve been trying to execute some command-line programs from JUCE with pipes such as ps aux | grep chrome and I want to launch it with juce::ChildProcess.

Somethin’ like:

juce::ChildProcess cp;
cp.start ("ps aux | grep chrome");
juce::String output = cp.readAllProcessOutput();
DBG ("Command Output:\n" + output + "\n");
uint32 exitCode = cp.getExitCode();

Problem is, the juce::ChildProcess does not seem to recognize the pipe character “|” as I am expecting. For the varying piped commands I attempted, I got either of these two errors:

  • The first command in the chain interprets the pipe character as an invalid argument.
  • The commands after the first pipe are completely ignored and I only receive output for the first command in the chain.

Is there any way to run commands piped together using juce::ChildProcess?

Do I need to launch these command chains via a call to bash or zsh?
I tried something like zsh -c 'ps aux | grep chrome', but no luck yet.

Thanks for any help!

maybe try

cp.start (StringArray ("zsh", "-c", "ps aux | grep chrome"))

Ah yes, that seems to do it! Thank you very much @chkn!

1 Like