2016-12-08 4 views

ответ

5

Вы можете сделать:

out, err := exec.Command("bash", "-c", "ps cax | grep myapp").Output() 
6

Переходя все к bash работает, но вот более идиоматических способ сделать это.

package main 

import (
    "fmt" 
    "os/exec" 
) 

func main() { 
    grep := exec.Command("grep", "redis") 
    ps := exec.Command("ps", "cax") 

    // Get ps's stdout and attach it to grep's stdin. 
    pipe, _ := ps.StdoutPipe() 
    defer pipe.Close() 

    grep.Stdin = pipe 

    // Run ps first. 
    ps.Start() 

    // Run and get the output of grep. 
    res, _ := grep.Output() 

    fmt.Println(string(res)) 
}