1 回答
TA贡献1789条经验 获得超10个赞
类似于以下内容:
func main() {
for ln := range topMon(2543) {
fmt.Println(time.Now().UTC().Format(time.RFC3339), ln)
}
}
func topMon(pids ...int) <-chan string {
ch := make(chan string, 1)
top := exec.Command("top", "-b")
for _, pid := range pids {
top.Args = append(top.Args, "-p", strconv.Itoa(pid))
}
r, w, _ := os.Pipe()
go func() {
sc := bufio.NewScanner(r)
for sc.Scan() {
ch <- sc.Text()
}
close(ch)
}()
top.Stdout = w
top.Stderr = os.Stderr
if err := top.Start(); err != nil {
panic(err)
}
return ch
}
通道使用只是一个示例,您可以直接从管道中返回 rwader。
- 1 回答
- 0 关注
- 205 浏览
添加回答
举报
