0

I have a question.

I need to do a bash script command that show me CPU usage and stamp in an external txt file.

Have some idea? Thank all so much!

NikM
  • 327
  • 1
  • 5
  • 12
  • BTW, what is your idea on this? – Guru Nov 01 '12 at 15:42
  • Take a look at this [question](http://stackoverflow.com/questions/9229333/how-to-get-overall-cpu-usage-e-g-57-on-linux) to see how to obtain the CPU usage. You'll just need to put it on a script, then you can run such as: `./cpuUsage.sh > cpu.txt`. – Yamaneko Nov 01 '12 at 15:43
  • Im running a script with some xterm. I need to calculate the cpu usage and print it into a text file – NikM Nov 01 '12 at 15:44

2 Answers2

2

You can use sysstat as suggested in the comments or stick with (probably installed already) top. Example from my system (you can of course grep out a specific field if needed):

$ top -bn 1 | sed -n '3p'
%Cpu(s): 16.4 us,  3.1 sy,  0.0 ni, 79.0 id,  1.3 wa,  0.0 hi,  0.2 si,  0.0 st

You can add the timestamp with date:

$ date
Thu Nov  1 19:46:15 MSK 2012
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
0

The top version and sysstat never worked for me. This did:

ps -A -o pcpu | tail -n+2 | paste -sd+ | bc
RFon
  • 118
  • 8