Problems with os_cmd

per@REDACTED per@REDACTED
Wed Oct 17 16:30:39 CEST 2001


Hi,

try using the "stderr_to_stdout" option in open_port.
On the topic of options I think you should use the "exit_status" option as well
so you can determine if the command returned error.

Regards,
Per

Nico Weling wrote:

> Hi all,
>
> I'm using the os_cmd program (thanks to Ulf) which is shown at the end of this mail. If I execute kermit with os_cmd I always get only the last two lines. If I execute kermit from the unix-shell I have the important information for filesize etc.
>
> Why don't I get this infomation with the os_cmd programm?
>
> Thanks in advance,
>
> best regards/Nico.
>
> ========================================================================================
>
> Eshell V4.9.1  (abort with ^G)
> 1>  gmc_util:os_cmd("kermit /etc/kermit/r520.cfg -l /dev/ms1 -r -a /home/data_receive/").
> Bytes: "\r\n"
> Bytes: "Closing /dev/ms1...OK\n"
> "\r\nClosing /dev/ms1...OK\n"
>
> ========================================================================================
>
> [root@REDACTED gmc]# sh -c 'kermit /etc/kermit/r520.cfg -l /dev/ms1 -r -a /home/data_receive/' 2>&1
>  RECV hosts (binary) (901 bytes): OK (4.079 sec, 220 cps)
>
> Closing /dev/ms1...OK
>
> [root@REDACTED gmc]#
>
> ========================================================================================
>
> gmc_util.erl:
>
> os_cmd(Cmd) ->
>     Command = binary_to_list(
>                 list_to_binary(["sh -c '", Cmd,
>                                 "' 2>&1"])),
>     Port = open_port({spawn, Command}, [stream, eof]),
>     get_data(Port, []).
>
> get_data(Port, Sofar) ->
>     receive
>         {Port, {data, Bytes}} ->
>             catch ?gmc_logger ! {log, "util ~s: Sofar: ~p~n",
>                                  [version(), Bytes]},
>             io:format("Bytes: ~p~n",[Bytes]),
>             get_data(Port, Sofar ++ Bytes);
>
>         {Port, eof} ->
>             Port ! {self(), close},
>             receive
>                 {Port, closed} ->
>                     true
>             end,
>             receive
>                 {'EXIT', Port, _} ->
>                     ok
>             after 1 ->
>                     ok
>             end,
>             Sofar;
>
>         {'EXIT', Port, _} ->
>             Sofar
>
>     after 300000 -> %wait 5mins before exit
>             exit(timeout)
>     end.




More information about the erlang-questions mailing list