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!

3
  • BTW, what is your idea on this? Commented Nov 1, 2012 at 15:42
  • Take a look at this question 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. Commented Nov 1, 2012 at 15:43
  • Im running a script with some xterm. I need to calculate the cpu usage and print it into a text file Commented Nov 1, 2012 at 15:44

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

ok its work. How can i print this out in txt file with bash? Thank you so mucH!
@NikM Just add a redirection in the end (>> log.txt for example).
Yes its work:) Sorry i have used 2> and doesnt work. Thanks a lot!
0

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

ps -A -o pcpu | tail -n+2 | paste -sd+ | bc

1 Comment

@jangorecki sudo apt-get install bc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.