I am exploring ways of running external programs via Elixir, and to my surprise I didn’t found a way to read from called program’s stdout and stderr separately.
It seems that this is related to how Erlang primitives are handling that task.
Only thing I found, is open_port/2’sstderr_to_stdout option, to merge both streams, and it seems Elixir’s Port.open does the same.
I found some external modules for Erlang supposedly addressing my need, but my question would be: is there elegant / native way to do it in Elixir?
Alternatively, is there a way to call bare sysalls on POSIX platform?
P.S. I wasn’t really sure where to post that question or how exactly label / tag it - my apologies if something went wrong here!
With my current knowledge of Elixir I guess that I can try to achieve what I want with - for example - 2 named pipes or named pipe plus stderr or stdout? All the moving parts around it seem to me bit intimidating.
For note, Port's are more for running with programs designed to work with your BEAM program, it’s less for running ‘other’ things as it doesn’t have this control over file descriptors, PID handling, or anything. If you want to manage programs that aren’t designed to be Port programs then I recommend using something like erlexec or so as it uses a shim C program to act as the binding layer between a Port call and Everything else.
Thank you for the reply, much appreciated @OvermindDL1.
Yes, that’s the module I’ve found, but I was searching something more lightweight, native, so to speak, but after your post I think I understand situation better.
As a side-note - it feels quite exotic to me, I’ve never encountered such design during my adventures with other programming languages; it’s interesting.
The only way to do mutability on the BEAM (short of some hacks) is via message passing, and Ports are just “Message Passing” back and forth to an external process designed for it, so it fulfills that model.
Multiple paths of streams doesn’t. So a hoisting program or NIF or something can fill that void by wrapping a higher level layer into the message passing structure. Which is what erlexec does.
Yeah the BEAM is not so much a VM like Java, it’s designed to pretend to be it’s own standalone operating system internally within it. ^.^
You can also look at porcelain with or without goon plugin which despite its age still works well for me when I needed stdin control beyond what System.cmd can provide without digging into low-level Ports.
That said erlexec is better maintained and I found (but not tried) a more idiomatic Elixir wrapper for it exexec
Yeah, I’ve had good success with erlexec in the past. Just used it directly, wasn’t too bad, although I do remember running into a bump or two configuring it.