I need to run following command on Linux and get output using golang.
/bin/ps o pid,%cpu,%mem -p 14806
command works fine and produces result as follows:
PID %CPU %MEM
14806 0.8 6.0
but it is not working via golang code
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
cmd := exec.Command("ps", "o pid,%cpu,%mem -p 14806")
fmt.Printf("Path: %q, args[1]: %q
", cmd.Path, cmd.Args[1])
out, err := exec.Command("ps", "o pid,%cpu,%mem -p 14806").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("The date is %s
", out)
}
output
Path: "/bin/ps", args[1]: "o pid,%cpu,%mem -p 14806"
2019/05/16 07:23:17 exit status 1
exit status 1
Thanks @Volker
following code works
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
cmd := exec.Command("ps", "--no-headers", "o", "pid,%cpu,%mem", "-p", "14806")
fmt.Printf("Path: %q, args[1]: %q
", cmd.Path, cmd.Args[1])
out, err := exec.Command("ps", "--no-headers", "o", "pid,%cpu,%mem", "-p", "14806").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s
", out)
}