Execute external program and get the output

I would like to execute command on the host machine. I am using the port Module.
I know the file gets executed because the php script is creating log files. But i dont know how to receive the output back to elixir. I can see the data php script outputed only when i use the flush command.

How can i get the data my external programm outputs?

port = Port.open({:spawn, "php filename.php"}, [:binary])
flush()
{#Port<0.14>, {:data, "some data"}}

If you just need to run the script and get output, use System.cmd/3. Examples are provided in function documentation, and some notices.

Just receive until you get a message telling you about the exit of the port.

Though as @vfsoraki said, using System.cmd/3 might be more what you actually want.

Alternatively you can use porcelain or rambo.

2 Likes

I have tryed System module and the porcelain library, they all work similary to the Port module and the same problem remains.
I can execute my external command. But for some reason I am not getting the output of the exectued script.
However if I use the flush() in elixir I can actualy see the output : {#Port<0.6>, {:data, "some data"}}
Is there any other way besides using the flush command to get the outputed data?

As I said, you can use receive to receive the ports messages manually.

Though I’m wondering why neither System.cmd/3 nor porcelain give you the output, perhaps your tool is only printing output after its stdin has been closed? Then this is exactly what rambo was made for, give it a try.

1 Like

Thank you all for the answers. I can receive messages from the port with the following code:
receive do {_, {:data, msg}} -> IO.puts(msg) end