-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
exec.Command cannot get all stdout message #38268
Comments
This looks racey. You might just need to wait for the goroutines to finish printing their output before returning from |
Also:
|
Per above, doesn't seem to be a bug in Go here. The program needs to delay the call to |
@ianlancetaylor i don't think so. the program is still running and keep reading from |
have you try running my code above? |
Your code doesn't compile due to an unused import. But even if you fix that, running your code is pretty pointless since it's doing something that the docs explicitly say you shouldn't do. Code always needs to be well-formed to be a meaningful reproduction of an issue. This is well-formed: package main
import (
"io"
"log"
"os"
"os/exec"
"sync"
)
func execute() error {
cmd := exec.Command("git", "clone", "https://github.com/dotnet/runtime.git")
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
if err := cmd.Start(); err != nil {
log.Printf("Error executing command: %s......\n", err.Error())
return err
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
io.Copy(os.Stdout, stdout)
}()
wg.Add(1)
go func() {
defer wg.Done()
io.Copy(os.Stderr, stderr)
}()
wg.Wait()
if err := cmd.Wait(); err != nil {
log.Printf("Error waiting for command execution: %s......\n", err.Error())
return err
}
log.Printf("end")
return nil
}
func main() {
execute()
} Also note: The output of Your There is an easier way to pipe to your stdout/stderr. package main
import (
"log"
"os"
"os/exec"
)
func execute() error {
cmd := exec.Command("git", "clone", "https://github.com/dotnet/runtime.git", "--progress")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
log.Printf("Error executing command: %s......\n", err.Error())
return err
}
if err := cmd.Wait(); err != nil {
log.Printf("Error waiting for command execution: %s......\n", err.Error())
return err
}
return nil
}
func main() {
execute()
} |
@ccbrown first of all, thanks for pointing out the issues of my code! after testing a couple of times, my conclusion is it may be a git issue, because if i run the git clone command under terminal directly, i don't need to set |
@liesauer I had the same problem in another language today, and I've found a clear explanation here, FYI https://stackoverflow.com/questions/35700994/how-to-capture-git-clone-output-with-fork-exec |
right, but i don't understand why git outputs the details to |
What version of Go are you using (
go version
)?go version go1.13.4 darwin/amd64
What did you do?
try to run git command using golang and get the realtime output.
What did you expect to see?
remote message and progress is missing.
What did you see instead?
and nothing more
The text was updated successfully, but these errors were encountered: