One simple way to do this is the following:
-module(tport).
-export([ execute/2 ]).
execute(Host, Cmd) ->
Port = open_port({spawn, Cmd}, [ {cd, Host}, exit_status, binary ] ),
wait(Port).
wait(Port) ->
receive
{Port, {data, BinData}} ->
io:format("dump:~n~p~n", [BinData]),
wait(Port);
{Port, {exit_status, Status}} ->
io:format("exit_code: ~p~n", [Status]);
%% {Port, eof} ->
%% port_close(Port);
{Port, exit} ->
io:format("Received : ~p~n", [Port])
end.
Once a port opened your process will receive various messages and one we're interested in is the 'exit_status' one:
{Port, {exit_status, Status}} ->
io:format("exit_code: ~p~n", [Status]);
The variable 'Status' will hold the exit code.
Simple isn't it ?
1 comment:
This is very interesting and looks like just what I needed to know. Keep up the posts! :)
Post a Comment