Silencing output of System.cmd/3?

I have some tests where I shell out to execute a Java app using System.cmd/3. This app is very verbose and writes to STDOUT, is there a way to silence it?

I’m looking for something like capture_log, but capture_io only seems to silence IO, but not the output from system commands.

Usually all output of the called command shut be captured and returned to you and nothing printed to stdout… Can you therefore show an example of a call that actually prints to stdout?

You can pass stderr_to_stdout: true as 3rd argument to System.cmd/3 and both stdout & stderr will be return value of the call.

I’m way out of my depth here but since System.cmd is using a port perhaps ex_unit can’t capture it’s IO.

1 Like

stderr_to_stdout: true won’t change anything, as the OP said it is printed to STDOUT.

By default it’s just returned, so you can ignore it. Can you give an example usage via the iex terminal with some posix command or so to act as a demonstration of what it does and explain what you want it to do instead?

Thanks everyone!

It turns out that this Java app was writing both to STDOUT and STDERR, so the solution given by @wojtekmach fixed the issue, redirecting STDERR to STDOUT made it silent as the output is just returned, not printed.


AFAIK, System.cmd/3 returns {captured_text_in_stdout, exit_code}. How can I capture both stdout and stderr separately?

You can’t.

If you want to capture them separately, you need to use Ports or one of the many portwrapper libraries like porcelain or rambo.

Be aware though, that both of them require additional programs on your system to wrap the actual command. I’m not sure if they get delivered/bundled with the package or if they need to be installed separately.


Many thanks. I tried rambo and it seems it does not require additional programs/software.