[erlang-questions] Executing external commands

Denis Bilenko denis.bilenko@REDACTED
Mon Feb 12 10:57:22 CET 2007


With similar code, sometimes I don't receive {P, {exit_status, ..}.
I'm using windows, so it may be windows-specific. I've added after
clause to prevent hanging:

get_data(P, D) ->
    receive
        {P, {data, D1}} ->
            get_data(P, [D|D1]);
        {P, eof} ->
            port_close(P),
            Status = receive
                           {P, {exit_status, N}} -> N;
                           after 1 -> timeout
                           end,
            {Status, lists:reverse(D)}
            end
    end.

It would be interesting to know why a message can get lost. (or why
open_port forget to send one). There's no heavy load and flush()
doesn't report lost messages.

On 2/12/07, Richard Carlsson <richardc@REDACTED> wrote:
> You can roll your own version of command/1. I use a piece of code
> like the following to run external commands in EUnit:
>
> command(Cmd) ->
>     Opt = [stream, exit_status, use_stdio,
>            stderr_to_stdout, in, eof],
>     P = open_port({spawn, Cmd}, Opt),
>     get_data(P, []).
>
> get_data(P, D) ->
>     receive
>         {P, {data, D1}} ->
>             get_data(P, [D|D1]);
>         {P, eof} ->
>             port_close(P),
>             receive
>                 {P, {exit_status, N}} ->
>                     {N, lists:reverse(D)}
>             end
>     end.
>
>    /Richard



More information about the erlang-questions mailing list