Jimmix
Jimmix

Reputation: 6506

How can I store the output of "top" instead of having it print to the terminal?

If you type into console:

top

you get the whole terminal screen filled with the application.

But what I'm interested in is the output of top stored into a variable instead of top taking display of my console.

How to use top in sh or bash shell to get the top app print stored into a variable that later can be echoed?

I don't need to keep the top alive after getting its output, it may be called again if I need updated values;.

Upvotes: 1

Views: 548

Answers (1)

Benjamin W.
Benjamin W.

Reputation: 52132

Some versions of top have a batch mode, which can be enabled with top -b. It'll run for the number of iterations set with -n, or until killed.

For example, to get a snapshop of the current top five most CPU intensive processes, you'd use

top -b -n 1 -o +%CPU | head -n 5 > snapshot

Upvotes: 4

Related Questions