How to get the realtime output for a shell command in golang?

前端 未结 4 564
渐次进展
渐次进展 2020-12-25 14:35

I am trying to call shell command with os/exec in golang, that command will take some time, so I would like to retrieve the reatime output and print the processed output (a

4条回答
  •  隐瞒了意图╮
    2020-12-25 15:04

    When you have an exec.Cmd value of an external command you started from Go, you may use its Cmd.Stdin, Cmd.Stdout and Cmd.Stderr fields to communicate with the process in some way.

    Some way means you can send data to its standard input, and you can read its standard output and error streams.

    The stress is on standard. If the external process is sending data on a network connection, or is writing data to a file, you will not be able to intercept those data via the above mentioned 3 streams.

    Now on to ffmpeg. ffmpeg and many other console applications do not write data to standard output/error, but they use system calls or other libraries (that use system calls) to manipulate the terminal window. Of course an application may send some data to the standard output/error, and may display other data by manipulating the terminal window.

    So you don't see the output of ffmpeg because you try to read its standard output/error, but ffmpeg does not display its output by writing to those streams.

    In the general case if you want to capture the output of such applications, you need a library that is capable of capturing the (textual) content of the terminal window. In an easier situation the application supports dumping those output to files usually controlled by extra command line parameters, which then you can read/monitor from Go.

提交回复
热议问题