Core.Unix.create_process: How can I see the output?

create_process returns a Unix.Process_info.t, which contains file descriptors corresponding to each of the standard streams of the newly spawned process. So, for example, you could do something like:

let () =
  let process_info = Unix.create_process ~prog ~args in
  let child_stderr = Unix.in_channel_of_descr process_info.stderr in
  In_channel.input_line child_stderr ...

In this case, create_process already created pipes to the child process instead of you having to do that like you would with Stdlib.Unix.create_process, but it means that if you wanted to share the file descriptors, you have to do it manually.

Alternatively, I suppose you could try to do something with Unix.dup2, but that seems racy?

You could also use the spawn library, which Core.Unix.create_process is built on top of, and which allows you to specify the file descriptors directly, as you would with Stdlib.Unix.create_process.

3 Likes