Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to continuously show os command output in erlang?
    text
    copied!<p>I need continuously show stdout/stderr from os command in erlang. In ruby I can implement it with following code:</p> <pre><code>s1, s2 , s3, t = Open3.popen3('for %a in (1 2 3 4 5 6 7 8 9) do (echo message &amp; sleep 2 ) 2&gt;&amp;1 ') s2.each do |l| puts l end </code></pre> <p>it will show 'message\n message\n' in 'real time' - does not wait end of process.</p> <p>I've tried <code>os:cmd(..)</code> and </p> <pre><code>1&gt; P5 = erlang:open_port({spawn, "ruby rtest.rb"}, [stderr_to_stdout, in, exit_s tatus, binary,stream, {line, 255}]). #Port&lt;0.505&gt; 2&gt; receive {P5, Data} -&gt; io:format("Data ~p~n",[Data]) end. Data {data,{eol,&lt;&lt;&gt;&gt;}} ok </code></pre> <p>but both of them wait end of process.</p> <p>Are the any optional for continuously stdout reading in Erlang?</p> <p><strong>EDIT:</strong> In other words I look for a popen (c/c++; proc_open(php) and etc) function in erlang</p> <p><strong>EDIT2</strong> Code, that works on linux (tested on centos6.2). Thanks for <code>vinod</code>:</p> <pre><code>-module(test). -export([run/0]). run() -&gt; P5 = erlang:open_port({spawn, "sh test.sh"}, [stderr_to_stdout, in, exit_status,stream, {line, 255}]), loop(P5). loop(P) -&gt; receive{P, Data} -&gt; io:format("Data ~p~n",[Data]), loop(P) end. </code></pre> <p>Output:</p> <pre><code>10&gt; c(test). {ok,test} 11&gt; test:run(). Data {data,{eol,"1"}} Data {data,{eol,"2"}} Data {data,{eol,"3"}} Data {data,{eol,"4"}} Data {data,{eol,"5"}} Data {data,{eol,"6"}} Data {data,{eol,"7"}} Data {data,{eol,"8"}} Data {data,{eol,"9"}} Data {data,{eol,"10"}} Data {exit_status,0} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload