calling command with some arguments works but not with others but works from console

Separate your arguments.

top -o cpu -n 10 -l 2 is not what you are executing. What you’re passing as arguments to the command is equivalent to using top "-o cpu" "-n 10" "-l 2" in a shell (which if you try, it will give you the exact same output).

Most commands will strictly parse that as 3 arguments. Since POSIX arguments don’t require a space, top splits off the -o as the first option, and uses the rest as its argument. This works for the numerical arguments mostly by accident, but the for -o it looks for a field named " cpu", which there isn’t.

Instead, use

exec.Command(app, "-o", "cpu", "-n", "10", "-l", "2")

Leave a Comment