calling cmd

102 views
Skip to first unread message

Jeff Mangan

unread,
Jan 28, 2021, 12:29:14 PM1/28/21
to golang-nuts
I am trying to get the results of top (more specifically htop)  but every time it prints nothing, whereas any other command (ls, pwd, etc...) returns the output fine.  My objective is to get access to the process stats that are returned to the screen.

Here is the latest example which displays nothing:

package main

import (
"fmt"

"github.com/go-cmd/cmd"
)

func main() {
// Create Cmd, buffered output
envCmd := cmd.NewCmd("htop")

// Run and wait for Cmd to return Status
status := <-envCmd.Start()

// Print each line of STDOUT from Cmd
for _, line := range status.Stdout {
fmt.Println(line)
}
}

Ian Lance Taylor

unread,
Jan 28, 2021, 1:00:55 PM1/28/21
to Jeff Mangan, golang-nuts
On Thu, Jan 28, 2021 at 9:29 AM Jeff Mangan <je...@magentatech.com> wrote:
>
> I am trying to get the results of top (more specifically htop) but every time it prints nothing, whereas any other command (ls, pwd, etc...) returns the output fine. My objective is to get access to the process stats that are returned to the screen.
>
> Here is the latest example which displays nothing:

Please post code as plain text or as a link to the Go playground.
Colorized text on a black background is difficult to read for many
people, but everyone who uses e-mail can see plain text. Thanks.

I'm not familiar with the htop program. What happens if you run "htop
> out.txt"? What winds up in out.txt?

Ian

Carla Pfaff

unread,
Jan 28, 2021, 1:06:27 PM1/28/21
to golang-nuts
"htop" doesn't exit (the channel read blocks until exit), and it uses ANSI escape codes for colorful output. You'd be better served by "top -b -n 1" (Linux) or  "top -l1" (macOS).

Kurtis Rader

unread,
Jan 28, 2021, 4:08:56 PM1/28/21
to Jeff Mangan, golang-nuts
On Thu, Jan 28, 2021 at 9:29 AM Jeff Mangan <je...@magentatech.com> wrote:
I am trying to get the results of top (more specifically htop)  but every time it prints nothing, whereas any other command (ls, pwd, etc...) returns the output fine.  My objective is to get access to the process stats that are returned to the screen.

Note that htop is screen oriented, not line oriented. That is, it uses terminal escape sequences to move the cursor and change the color of the text. Run

echo q | htop > x

Then look at the content of file "x".  You won't see lines of text like you would from the output of a command like ls. You could use Go package github.com/creack/pty to communicate with htop via a pseudo-tty and "screen scrape" its output. But that would be a really painful way to get information about running processes. It's hard to provide useful advice because it appears you've made the "XY Problem" mistake. Depending on what information you really want and the platform you're running on the answer may be as simple as running "ps waux" and capturing the %CPU column.

--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Jeff Mangan

unread,
Jan 28, 2021, 4:48:08 PM1/28/21
to Kurtis Rader, golang-nuts
XY Problem.... I'll be using that in the future :-)

Yeah, I just wanted to see those stats and thought I would write a go app to do it just for the sake of writing some go code.  It's not important.  Thanks for the info.
Reply all
Reply to author
Forward
0 new messages