<div dir="ltr"><font face="monospace, monospace">My ultimate goal is to run two external processes, with the output of one feeding into the input of the other, a la shell piping. So something like `zfs send | zfs recv` or `tar | split`, but from within my Erlang application. Below is my simple example that fairly quickly balloons memory out of control and eventually explodes (i.e. the OS kills it).<br><br>#!/usr/bin/env escript<br>%%!<br><br>main(_Args) -><br>  Â  SendCmd = "cat /dev/random",<br>  Â  %<br>  Â  % not using 'binary' with open_port makes memory size grow really fast,</font><div><font face="monospace, monospace">  Â  % but that's fine, I want that option anyway, just pointing it out<br>  Â  %<br>  Â  SendPort = erlang:open_port({spawn, SendCmd}, [exit_status, binary]),<br>  Â  RecvCmd = "strings",<br>  Â  RecvPort = erlang:open_port({spawn, RecvCmd}, [exit_status, binary]),<br>  Â  {ok, 0} = pipe_until_exit(SendPort, RecvPort, 0),<br>  Â  ensure_port_closed(SendPort),<br>  Â  ensure_port_closed(RecvPort),<br>  Â  ok.<br><br>pipe_until_exit(SendPort, RecvPort, N) -><br>  Â  %<br>  Â  % invoking garbage_collect/0 helps a little bit, but memory still grows<br>  Â  %<br>  Â  io:format("iteration ~w~n", [N]),<br>  Â  receive<br>  Â  Â  Â  {SendPort, {exit_status, Status}} -><br>  Â  Â  Â  Â  Â  {ok, Status};<br>  Â  Â  Â  {RecvPort, {exit_status, Status}} -><br>  Â  Â  Â  Â  Â  io:format("error: receive port exited ~w~n", [Status]);<br>  Â  Â  Â  {SendPort, {data, Data}} -><br>  Â  Â  Â  Â  Â  true = erlang:port_command(RecvPort, Data),<br>  Â  Â  Â  Â  Â  pipe_until_exit(SendPort, RecvPort, N + 1);<br>  Â  Â  Â  {RecvPort, {data, _Data}} -><br>  Â  Â  Â  Â  Â  pipe_until_exit(SendPort, RecvPort, N + 1);<br>  Â  Â  Â  Msg -><br>  Â  Â  Â  Â  Â  io:format("some other message: ~w", [Msg])<br>  Â  end.<br><br>ensure_port_closed(Port) -><br>  Â  case erlang:port_info(Port) of<br>  Â  Â  Â  undefined -> ok;<br>  Â  Â  Â  _ Â  Â  Â  Â  -> erlang:port_close(Port)<br>  Â  end.<br></font><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">My assumption is that I am doing something wrong, but I can't see it. I'll explore other solutions, such as simply generating a shell script and invoking it. In the mean time, if you can see something obvious, please let me know.</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">Yes, I am aware of os:cmd/1, but that does not return the exit code, which I really want to have so I know that something at least appeared to work (zfs send|zfs recv and tar|split do not generate any success indication other than exit code).</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">Thanks</font></div></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">n</font></div><div><br></div></div>