将cmd stdout和stderr作为字符串返回,而不是在golang

时间:2015-06-28 02:52:29

标签: go

我正在从golang应用程序执行bash命令。现在stdoutstderr直接转到控制台:

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

但我希望stdoutstderr作为字符串变量从runBashCommandAndKillIfTooSlow函数返回,而不立即打印到控制台。 如何实现这个?

代码:

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
}

顺便说一句,如果程序太慢(killInMilliSeconds参数),程序应该保留杀死bash命令的能力。

3 个答案:

答案 0 :(得分:12)

将输出设置为bytes.Buffer

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

运行该命令后,您可以通过调用Buffer.String()方法将stdout和stderr作为字符串获取:

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

答案 1 :(得分:2)

您可以使用cmd.Run()代替cmd.Start()使其自动等待它完成,并使用exec.CommandContext()使其超时,从而简化此操作。这也将以正确的顺序输出,而原始程序由于例程而无序。

这是完全相同的程序简化并使用@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 :(得分:0)

另一个选项是strings.Builder

package main

import (
   "os/exec"
   "strings"
)

func main() {
   b := new(strings.Builder)
   c := exec.Command("go", "version")
   c.Stdout = b
   c.Run()
   println(b.String() == "go version go1.16.3 windows/amd64\n")
}

https://golang.org/pkg/strings#Builder