Return cmd stdout and stderr as a string instead of printing to the console in golang

I am executing bash commands from an application golang. Now stdoutand stderrgo directly to the console:

cmd.Stdout = os.Stdout 
cmd.Stderr = os.Stderr

But I would like to stdout, and stderrreturned in the form of string variables from the function runBashCommandAndKillIfTooSlowwithout immediately printing on the console. How to implement this?

The code:

package main

import (
    "fmt"
    "log"
    "os"
    "os/exec"
    "time"
)

func main() {
    ok, outString, errString := runBashCommandAndKillIfTooSlow("ls -la", 2000)
    fmt.Println("ok")
    fmt.Println(ok)
    fmt.Println("outString")
    fmt.Println(outString)
    fmt.Println("errString")
    fmt.Println(errString)
}

/*
run bash command and kill it if it works longer than "killInMilliSeconds" milliseconds
*/
func runBashCommandAndKillIfTooSlow(command string, killInMilliSeconds time.Duration) (okResult bool, stdout, stderr string) {
    fmt.Println("running bash command...")
    fmt.Println(command)
    cmd := exec.Command("sh", "-c", command)

    cmd.Stdout = os.Stdout // cmd.Stdout -> stdout
    cmd.Stderr = os.Stderr // cmd.Stderr -> stderr

    okResult = true

    err := cmd.Start()
    log.Printf("Waiting for command to finish...")
    done := make(chan error, 1)
    go func() {
        done <- cmd.Wait()
    }()
    select {
    case <-time.After(killInMilliSeconds * time.Millisecond):
        if err := cmd.Process.Kill(); err != nil {
            log.Fatal("failed to kill: ", err)
            okResult = false
        }
        <-done // allow goroutine to exit
        // log.Println("process killed")
    case err := <-done:

        if err != nil {
            log.Printf("process done with error = %v", err)
            okResult = false
        }
    }
    if err != nil {
        log.Fatal(err)
        okResult = false
    }
    return
}

By the way, the program should retain its ability to kill the bash command if it was too slow (parameter killInMilliSeconds).

+4
source share
2 answers

Set the output bytes.Buffer :

var outbuf, errbuf bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf

After running the command, you can get stdout and stderr as a string by calling the Buffer.String () method :

stdout := outbuf.String()
stderr := errbuf.String()
+9

, cmd.Run() cmd.Start(), , , exec.CommandContext(), . , - go.

@Mello Marmot:

package main

import (
    "bytes"
    "fmt"
    "log"
    "os"
    "os/exec"
    "time"

    "golang.org/x/net/context"
)

func main() {
    ctx := context.Background()
    ok, outString, errString := runBashCommandAndKillIfTooSlow(ctx, "ls -la", 2000*time.Millisecond)
    fmt.Println("ok")
    fmt.Println(ok)
    fmt.Println("outString")
    fmt.Println(outString)
    fmt.Println("errString")
    fmt.Println(errString)
}

/*
run bash command and kill it if it works longer than "killIn"
*/
func runBashCommandAndKillIfTooSlow(ctx context.Context, command string, killIn time.Duration) (okResult bool, stdout, stderr string) {
    fmt.Println("running bash command...")
    fmt.Println(command)
    ctx, _ = context.WithTimeout(ctx, killIn)
    cmd := exec.CommandContext(ctx, "sh", "-c", command)

    // Set output to Byte Buffers
    var outb, errb bytes.Buffer
    cmd.Stdout = &outb
    cmd.Stderr = &errb

    okResult = true
    err := cmd.Run()
    stdout = outb.String()
    stderr = errb.String()
    if err != nil {
        log.Fatal(err)
        okResult = false
    }
    return
}
+2

All Articles