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.

Jay
  • 113
  • Have you tried mpstat -P all ? This gives similar output to the CPU section in to. – Simply_Me Aug 10 '14 at 01: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 Aug 10 '14 at 02:00
  • Great! I'm looking into top, but so far didn't no luck. – Simply_Me Aug 10 '14 at 02:04
  • There are actually several various implementations of top. Which are you using? You can typically find out with top -v. – slm Aug 10 '14 at 02:47
  • Use batch mode as described is this answer https://unix.stackexchange.com/questions/60328/how-to-stream-top-snapshot-into-file?noredirect=1&lq=1 – Yu Jiaao Mar 03 '18 at 13:55

1 Answers1

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.

Simply_Me
  • 1,752
  • 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 Aug 10 '14 at 03:30
  • @Jayraj let me know if i can be of further help. – Simply_Me Aug 10 '14 at 04:05