1

I want redirect top output to a file, but I am interested in the per-CPU usage.

Doing just

#I only want to see processes belonging to myuser
top -b -d 1 -u myuser > top.txt

gives me stats with all the CPUs usage combined.

Running just top and then pressing 1 makes top break out per-CPU usage. How do I redirect this to the file?

I've seen some related questions:

How can I receive top-like CPU statistics from the shell?

How to stream top snapshot into file?

But not found the answer to how to make the part of the command before the redirect operator take the 1 that top needs for per-CPU stats.

5
  • Have you tried mpstat -P all ? This gives similar output to the CPU section in to.
    – Simply_Me
    Commented Aug 10, 2014 at 1:56
  • That works for me too! Thanks! Just of curiosity though, is there some way to do what I asked? There could be situations in future where one wants to redirect a program's output but needs to give it some input after running it but before needing to redirect.
    – Jay
    Commented Aug 10, 2014 at 2:00
  • Great! I'm looking into top, but so far didn't no luck.
    – Simply_Me
    Commented Aug 10, 2014 at 2:04
  • There are actually several various implementations of top. Which are you using? You can typically find out with top -v.
    – slm
    Commented Aug 10, 2014 at 2:47
  • Use batch mode as described is this answer unix.stackexchange.com/questions/60328/…
    – Yu Jiaao
    Commented Mar 3, 2018 at 13:55

1 Answer 1

1

I looked for sometime into top and there is no straight forward way to do this. As I mentioned earlier, you can use mpstat -P all > top.txt and then run your top command appending >> to output file for per user (you can use grep to filter... but that's a different topic :-). Can you elaborate what exactly the output you want to see (and is it part of an input for another script)?

Most likely you can use expect script to do that (interactively send 1 to top and capture the input), but I think it's an overkill.

2
  • I ended up using mpstat 1 -P all > top.txt. I'm trying to graph CPU usage to see how my app performs under high loads and whether it spreads the load across all CPUs evenly. The output data needs heavy massaging to have any spreadsheet program accept it though
    – Jay
    Commented Aug 10, 2014 at 3:30
  • @Jayraj let me know if i can be of further help.
    – Simply_Me
    Commented Aug 10, 2014 at 4:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .