operating system - How to continuously show os command output in erlang? -
i need continuously show stdout/stderr os command in erlang. in ruby can implement following code:
s1, s2 , s3, t = open3.popen3('for %a in (1 2 3 4 5 6 7 8 9) (echo message & sleep 2 ) 2>&1 ') s2.each |l| puts l end
it show 'message\n message\n' in 'real time' - not wait end of process.
i've tried os:cmd(..)
,
1> p5 = erlang:open_port({spawn, "ruby rtest.rb"}, [stderr_to_stdout, in, exit_s tatus, binary,stream, {line, 255}]). #port<0.505> 2> receive {p5, data} -> io:format("data ~p~n",[data]) end. data {data,{eol,<<>>}} ok
but both of them wait end of process.
are optional continuously stdout reading in erlang?
edit: in other words popen (c/c++; proc_open(php) , etc) function in erlang
edit2 code, works on linux (tested on centos6.2). vinod
:
-module(test). -export([run/0]). run() -> p5 = erlang:open_port({spawn, "sh test.sh"}, [stderr_to_stdout, in, exit_status,stream, {line, 255}]), loop(p5). loop(p) -> receive{p, data} -> io:format("data ~p~n",[data]), loop(p) end.
output:
10> c(test). {ok,test} 11> 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}
if understand correctly, want continue execute program in concurrent os command. in erlang can spawn process , can continue. example
1> spawn(fun() -> p5 = erlang:open_port({spawn, "ruby rtest.rb"}, [stderr_to_stdout, in, exit_status, binary,stream, {line, 255}]), receive {p5, data} -> io:format("data ~p~n",[data]) end end). <0.32.0> 2> 5+5. 10 3>
better write in module can understand better.
Comments
Post a Comment