Go version: go version go1.18 linux/arm64
What I expected: The program run script and return output result in each run.
What happen: The program run and sometimes does not return output.
I intentionally use cmd.Process.wait() because the code below is part of my application which needs to run script "service mysql start". If I used cmd.Wait(), my application would wait for mysql to finish and that is not what I want, so I use cmd.process.wait.
package main
import (
"fmt"
"bytes"
"os/exec"
)
func ExecCommand(script string) ([]byte, error) {
var buf bytes.Buffer
cmd := exec.Command("sh", "-c", script)
cmd.Stdout = &buf
cmd.Stderr = &buf
err := cmd.Start()
if err != nil {
return buf.Bytes(), err
}
_, err = cmd.Process.Wait()
return buf.Bytes(), err
}
func main() {
start_num := 118261738305877
for i:=0; i<50000; i++{
buf, err := ExecCommand(fmt.Sprintf("echo 'Y = %d' | grep 'Y' | awk '{print $3}'", start_num))
if err != nil {
fmt.Println("cmd execute err at round", i)
} else {
if len(buf) == 0 {
fmt.Println("cmd return nothing at round", i)
} else {
if i % 100 == 0 {
fmt.Println("cmd return ", string(buf), " at round ", i)
}
}
}
}
}
Here is my output on linux:
# go run test_go_process.go
cmd return 118261738305877
at round 0
cmd return 118261738305877
at round 100
cmd return nothing at round 115
cmd return 118261738305877
at round 200
cmd return 118261738305877
at round 300
cmd return 118261738305877
at round 400
cmd return 118261738305877
at round 2800
cmd return 118261738305877
at round 2900
cmd return 118261738305877
at round 3000
cmd return 118261738305877
at round 3100
cmd return 118261738305877
at round 3200
cmd return nothing at round 3290
cmd return 118261738305877
at round 3300
cmd return 118261738305877
output:cmd return nothing at round 115 shows that at this run, nothing return from func ExecCommand and I am wondering why and how to fix that except edit cmd.Process.wait(). Thanks a lot in advance.