In Erlang standard way to manage OS processes is ports.
Exit status of spawned OS process will be sent as a message if option exit_status
added to open_port:
1> P = open_port({spawn, "/bin/ls unknown_file_name"}, [exit_status]).
#Port<0.486>
/bin/ls: cannot access unknown_file_name: No such file or directory
2> flush().
Shell got {#Port<0.486>,{exit_status,2}}
ok
Data can be written to stdin of spawned OS process by sending a message to port or by port_command function and stdout content will be sent as a message. (Note also {line, L}
and {packet, N}
options of open_port
):
1> P = open_port({spawn, "/bin/cat"}, [stream, binary]).
#Port<0.486>
2> true = port_command(P, <<"data">>).
true
3> flush().
Shell got {#Port<0.486>,{data,<<"data">>}}
ok
4> true = port_close(P).
true
Stderr also can be redirected to stdout:
1> P = open_port({spawn, "/bin/ls unknown_file_name"}, [stderr_to_stdout, binary]).
#Port<0.486>
2> flush().
Shell got {#Port<0.486>,
{data,<<"/bin/ls: cannot access unknown_file_name: No such file or directory\n">>}}
ok
However you can't send kill signals with ports but if you close port by sending a message to port or by calling port_close external process can exit by SIGPIPE signal.