Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.cmd suppress output in Elixir

Tags:

elixir

I'm generating an RSA keypair using the openssl command line on Elixir, and everything works except I haven't been able to suppress the output of that command.

This is what I'm running:

{_, 0} = System.cmd "openssl", [ "genrsa", "-out", "privateKey.pem", "2048"]

and I keep getting:

Generating RSA private key, 2048 bit long modulus .....+++
.....................................+++ e is 65537 (0x10001)

After I compile with escript and run the executable.

like image 488
rausted Avatar asked Jul 10 '16 18:07

rausted


1 Answers

System.cmd collects standard output from the command and returns it, but what's happening here is that OpenSSL is writing to standard error, which by default isn't captured, and thus just gets printed to the terminal.

You can use the option stderr_to_stdout:

iex(2)> {_, 0} = System.cmd "openssl", [ "genrsa", "-out", "key.pem", "2048"],
                            [stderr_to_stdout: true]
{"Generating RSA private key, 2048 bit long modulus\n.......................+++\n......................................+++\ne is 65537 (0x10001)\n",
 0}

That means that the output will be returned in the first tuple element instead of printed to the terminal. Since you ignore that part of the return value, it won't be output when you run your program.

like image 150
legoscia Avatar answered Oct 21 '22 14:10

legoscia