How can I parse the output of exec.Command while still logging to os.Stdout in real time?


Unsung hero:

In a small Go application, I'm going to run a command using the os/exec package. Currently, I'm getting through the log output by setting the following:

cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()

What I want to do now is to get the output of the log (specifically the second to last line) and store it in a variable. I figured it would be just as easy to change the log string as err := cmd.Run()to cmdOutput, err := cmd.Output()the value I was looking for for parsing - however, this triggers an error saying exec: Stdout already set.

A setting I can remove cmd.Stdout, but I still want the rest of the log output to go through the current process the same way it does now.

I also have control over the program to be shelled, so other than writing the file to disk, if there's a better way to send JSON snippets from the subcommand/process to the parent Go app, that's fine too.

Any help would be greatly appreciated - I'm a little golang scrubbing...

Lyon:

You can use with :io.MultiWriterbytes.Buffer

var errBuf, outBuf bytes.Buffer
cmd.Stderr = io.MultiWriter(os.Stderr, &errBuf)
cmd.Stdout = io.MultiWriter(os.Stdout, &outBuf)
err := cmd.Run()

This will still log to os.Stdoutand os.Stderr, but you can errBuf.Bytes()use to get error output and outBuf.Bytes()get normal output. outBuf.Bytes()will give you a slice of bytes equal to the returned bytes cmd.Output().

Related


Printing stdout in exec command in real time in Go

Christian: I have a small Go tool that basically allows the user to define a command that will be run using os/exec. My problem is that I want to display the output (stdout/stderr) of the command to the user. An example might look like this: the user defines a

Printing stdout in exec command in real time in Go

Christian: I have a small Go tool that basically allows the user to define a command that will be run using os/exec. My problem is that I want to display the output (stdout/stderr) of the command to the user. An example might look like this: the user defines a

Printing stdout in exec command in real time in Go

Christian: I have a small Go tool that basically allows the user to define a command that will be run using os/exec. My problem is that I want to display the output (stdout/stderr) of the command to the user. An example might look like this: the user defines a

How do I run a command while logging the output to a file?

Ben Morel I have a console application that needs to run as part of the deployment of a new application version on the server. This console application is designed to output to the console and cannot be changed. I want to run it normally, but log stdout and st

How do I run a command while logging the output to a file?

Ben Morel I have a console application that needs to be run as part of the deployment of a new application version on the server. This console application is designed to output to the console and cannot be changed. I want to run it normally, but log stdout and

How can I get the output to a webpage while still in cfscript?

Rory Latham Sorry for the long post, I wanted to be specific. I'm a bit new to cold fusion and lucee, so forgive me if I'm missing some basics here. I'm just trying to make a quick POC, but can't get it to work. All I want to do is make phone calls, write web

How can I get the output to a webpage while still in cfscript?

Rory Latham Sorry for the long post, I wanted to be specific. I'm a bit new to cold fusion and lucee, so forgive me if I'm missing some basics here. I'm just trying to make a quick POC, but can't get it to work. All I want to do is make phone calls, write web

How can I get the output to a webpage while still in cfscript?

Rory Latham Sorry for the long post, I wanted to be specific. I'm a bit new to cold fusion and lucee, so forgive me if I'm missing some basics here. I'm just trying to make a quick POC, but can't get it to work. All I want to do is make phone calls, write web

How can I get the output to a webpage while still in cfscript?

Rory Latham Sorry for the long post, I wanted to be specific. I'm a bit new to cold fusion and lucee, so forgive me if I'm missing some basics here. I'm just trying to make a quick POC, but can't get it to work. All I want to do is make phone calls, write web

How can I get the output in real time in a subprocess?

Ricardo Oliveira: I tried to get tail -f /var/log/syslog to play the results in the variable data0 without success. from subprocess import Popen,PIPE def exit_data(): with Popen(['tail -f', '/var/log/syslog'],stdout=PIPE,stderr=PIPE) as b: out,er

How can I get the output in real time in a subprocess?

Ricardo Oliveira: I tried to get tail -f /var/log/syslog to play the results in the variable data0 without success. from subprocess import Popen,PIPE def exit_data(): with Popen(['tail -f', '/var/log/syslog'],stdout=PIPE,stderr=PIPE) as b: out,er

In Perl, how to clone output to stdout and log files in real time

Mudul Dohit I want to clone the output to the console as well as the log file at the same time. Here is my code snippet: open STDOUT, ">>", "temp.txt" or die ""; system("echo This is a sample output.........."); However, here I only get output on temp.txtand

In Perl, how to clone output to stdout and log files in real time

Mudul Dohit I want to clone the output to the console as well as the log file at the same time. Here is my code snippet: open STDOUT, ">>", "temp.txt" or die ""; system("echo This is a sample output.........."); However, here I only get output on temp.txtand