猿问

GO:使用管道执行命令

我想知道是否有一种方法可以top -b | grep --line-buffered [some_pid] >> out.log在 Go 中运行一段时间,然后在从通道接收到值后将其杀死。在os.exec似乎不支持命令管道。谢谢。


幕布斯6054654
浏览 128回答 1
1回答

慕姐8265434

这是我的管道示例,通过 OS Std Pipe 文件 a 调用文件 b,您可以对其进行编辑并添加计时器来执行您需要的操作。// apackage mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "os/exec"&nbsp; &nbsp; "runtime"&nbsp; &nbsp; "time")var cout chan []byte = make(chan []byte)var cin chan []byte = make(chan []byte)var exit chan bool = make(chan bool)func Foo(x byte) byte { return call_port([]byte{1, x}) }func Bar(y byte) byte { return call_port([]byte{2, y}) }func Exit() byte&nbsp; &nbsp; &nbsp; { return call_port([]byte{0, 0}) }func call_port(s []byte) byte {&nbsp; &nbsp; cout <- s&nbsp; &nbsp; s = <-cin&nbsp; &nbsp; return s[1]}func start() {&nbsp; &nbsp; fmt.Println("start")&nbsp; &nbsp; cmd := exec.Command("../b/b")&nbsp; &nbsp; stdin, err := cmd.StdinPipe()&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; stdout, err2 := cmd.StdoutPipe()&nbsp; &nbsp; if err2 != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err2)&nbsp; &nbsp; }&nbsp; &nbsp; if err := cmd.Start(); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; defer stdin.Close()&nbsp; &nbsp; defer stdout.Close()&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case s := <-cout:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stdin.Write(s)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buf := make([]byte, 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; runtime.Gosched()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(100 * time.Millisecond)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stdout.Read(buf)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cin <- buf&nbsp; &nbsp; &nbsp; &nbsp; case b := <-exit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if b {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Exit")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return //os.Exit(0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; go start()&nbsp; &nbsp; runtime.Gosched()&nbsp; &nbsp; fmt.Println("30+1=", Foo(30)) //30+1= 31&nbsp; &nbsp; fmt.Println("2*40=", Bar(40)) //2*40= 80&nbsp; &nbsp; Exit()&nbsp; &nbsp; exit <- true}文件 b:// bpackage mainimport (&nbsp; &nbsp; "log"&nbsp; &nbsp; "os")func foo(x byte) byte { return x + 1 }func bar(y byte) byte { return y * 2 }func ReadByte() byte {&nbsp; &nbsp; b1 := make([]byte, 1)&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; n, _ := os.Stdin.Read(b1)&nbsp; &nbsp; &nbsp; &nbsp; if n == 1 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return b1[0]&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func WriteByte(b byte) {&nbsp; &nbsp; b1 := []byte{b}&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; n, _ := os.Stdout.Write(b1)&nbsp; &nbsp; &nbsp; &nbsp; if n == 1 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; var res byte&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; fn := ReadByte()&nbsp; &nbsp; &nbsp; &nbsp; log.Println("fn=", fn)&nbsp; &nbsp; &nbsp; &nbsp; arg := ReadByte()&nbsp; &nbsp; &nbsp; &nbsp; log.Println("arg=", arg)&nbsp; &nbsp; &nbsp; &nbsp; if fn == 1 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res = foo(arg)&nbsp; &nbsp; &nbsp; &nbsp; } else if fn == 2 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res = bar(arg)&nbsp; &nbsp; &nbsp; &nbsp; } else if fn == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return //exit&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res = fn //echo&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; WriteByte(1)&nbsp; &nbsp; &nbsp; &nbsp; WriteByte(res)&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Go
我要回答