The following code works and outputs details of 10 processes.
package main
import (
"os/exec"
)
func main() {
print(top())
}
func top() string {
app := "/usr/bin/top"
cmd := exec.Command(app, "-n 10", "-l 2")
out, err := cmd.CombinedOutput()
if err != nil {
return err.Error() + " " + string(out)
}
value := string(out)
return value
}
However, when I try the same with an additional argument of "-o cpu" (e.g. cmd := exec.Command(app, "-o cpu", "-n 10", "-l 2")). I get the following error.
exit status 1 invalid argument -o: cpu
/usr/bin/top usage: /usr/bin/top
[-a | -d | -e | -c <mode>]
[-F | -f]
[-h]
[-i <interval>]
[-l <samples>]
[-ncols <columns>]
[-o <key>] [-O <secondaryKey>]
[-R | -r]
[-S]
[-s <delay>]
[-n <nprocs>]
[-stats <key(s)>]
[-pid <processid>]
[-user <username>]
[-U <username>]
[-u]
But the command "top -o cpu -n 10 -l 2" from my console works fine. Also I'm using OS X 10.9.3.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…