[erlang-questions] Running multiple commands like os:cmd/1

Siraaj Khandkar <>
Thu Dec 27 01:25:48 CET 2012

On Dec 26, 2012, at 3:37 PM, Gleb Peregud <> wrote:

> Happy holidays, everyone!
> I want to run multiple commands in a single shell from my code through
> some sort of open_port({spawn, ...}). I have four requirements:

Why does it have to be a single shell? What is wrong with multiple ports?

> 1) I want it to work asynchronously (i.e. I start a command and I get
> it's results streamed to a process as it happens);

If it really has to be a single shell, you can background each command
and you can tell when each will finish, but I don't know how would you
distinguish which output came from where.

> 2) I want to know exactly when a command finishes;
> 3) Binary output should be handled properly;
> 4) Changes in env variables should be preserved between commands.
> A naive sequence of os:cmd/1 or open_port({spawn, ...}) doesn't
> satisfy requirement 4.
> os:cmd/1 has a nice backend code in there, but it doesn't satisfy
> requirement 3. It has a bug. Try running length(os:cmd("head -c 1000
> /dev/urandom")).
> open_port({spawn, "/bin/sh -s -"}, [line]) with port_command/2 doesn't
> satisfy requirement 2.

How about open_port with 'exit_status' and call erlang:now/0 when receiving
{Port, {exit_status, ExitCode}} messages?

Siraaj Khandkar

More information about the erlang-questions mailing list