os_cmd(Command) ->
  Port = open_port({spawn, Command}, [stream, in, eof, hide, exit_status]),
  capture_cmd_output(Port, []).

capture_cmd_output(Port, Sofar) ->
  receive
    {Port, {data, Bytes}} ->
      capture_cmd_output(Port, [Sofar|Bytes]);

    {Port, eof} ->
      Port ! {self(), close},
      receive
        {Port, closed} ->
          true
      end,
      receive
        {'EXIT',  Port,  _} ->
          ok
      after 1 ->              % force context switch
          ok
      end,
      ExitCode =
        receive
          {Port, {exit_status, Code}} ->
            Code
        end,
      {ExitCode, lists:flatten(Sofar)}
  end.