We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Capturing top 20 CPU processes in Linux to a file

sunhux
sunhux asked
on
Medium Priority
488 Views
Last Modified: 2012-05-11

Hi,

If I issue "nohup top > /tmp/top.txt"
it will give all sorts of characters in the top.txt file

Is there any way I can capture a proper text output
(with the timestamp but without the unnecessary headers)
for top 20 CPU processes every 3 secs?
Comment
Watch Question

Commented:
This can give a Idea. This is for top 10 process and you can modify it for 20.

http://idolinux.blogspot.com/2008/08/see-your-top-ten-cpu-intensive.html

Author

Commented:

In fact "nohup top > /tmp/top.txt"  would not work at all

Author

Commented:

& the command below would prevent me from logoff the server:

top -d 3 -n 25000 -b > /var/tmp/top.txt

So I'll need something that allows me to logoff & it continues to capture

Commented:
for that you need to use nohup and & both.

# nohup <script.sh> &

It this way it will run after log off as well.

Author

Commented:


& I just need the top 20 CPU consuming processes but my previous command
gave practically all processes
CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Top Expert 2011
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Top Expert 2011
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Top Expert 2011
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:

 Omar's command would crash due to " | head -20" & it
would run Ok if " | head -20" is removed :

  "nohup top -d 3 -n 25000 -b | head -20 > /var/tmp/top.txt"


Upanwar script is missing looping 25000 times.


Wesly's script is missing 25000 loops too & there's a syntax error :
"while"   should   read    "while [ 1 ] "

Commented:
If you want to count it for 25000 time then It should work

#!/bin/bash

for (( c=1; c<=25000; c++ ))
do

ps -eo user,pcpu,pid,command | sort -r -k2 | head -21 >> /var/tmp/top20.txt

sleep 3

done
Top Expert 2011
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Ok
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.