Last active
March 19, 2025 15:44
-
-
Save hivefans/ffeaf3964924c943dd7ed83b406bbdea to your computer and use it in GitHub Desktop.
get the realtime output for a shell command in golang|-|{"files":{"shell_output.go":{"env":"plain"}},"tag":"bigdata"}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"os" | |
"os/exec" | |
"strings" | |
) | |
func main() { | |
cmdName := "ping 127.0.0.1" | |
cmdArgs := strings.Fields(cmdName) | |
cmd := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...) | |
stdout, _ := cmd.StdoutPipe() | |
cmd.Start() | |
oneByte := make([]byte, 100) | |
num := 1 | |
for { | |
_, err := stdout.Read(oneByte) | |
if err != nil { | |
fmt.Printf(err.Error()) | |
break | |
} | |
r := bufio.NewReader(stdout) | |
line, _, _ := r.ReadLine() | |
fmt.Println(string(line)) | |
num = num + 1 | |
if num > 3 { | |
os.Exit(0) | |
} | |
} | |
cmd.Wait() | |
} |
Nice !
Here is another example that I made based on the code posted above:
func RunCmd(ctx context.Context, cmdstr string) error {
args := strings.Fields(cmdstr)
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
var wg sync.WaitGroup
wg.Add(1)
scanner := bufio.NewScanner(stdout)
go func() {
for scanner.Scan() {
log.Printf("out: %s", scanner.Text())
}
wg.Done()
}()
if err = cmd.Start(); err != nil {
return err
}
wg.Wait()
return cmd.Wait()
}
Maybe you should use multiwritter:
cmd.Stdout = io.MultiWriter(os.Stdout, &stdout) cmd.Stderr = io.MultiWriter(os.Stderr, &stderr)
func main() {
cmd := exec.Command("echo", "test")
err := RunCommanad(cmd)
if err != nil {
// handle error
}
}
func RunCommand(cmd *exec.Cmd) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
return nil
}
This is the best solution that has worked for me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Still up to date, March 21 2022