• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 457
  • Last Modified:

Capturing top 20 CPU processes in Linux to a file


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?
0
sunhux
Asked:
sunhux
  • 5
  • 4
  • 4
  • +1
6 Solutions
 
upanwarCommented:
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
0
 
sunhuxAuthor Commented:

In fact "nohup top > /tmp/top.txt"  would not work at all
0
 
sunhuxAuthor 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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

# nohup <script.sh> &

It this way it will run after log off as well.
0
 
sunhuxAuthor Commented:


& I just need the top 20 CPU consuming processes but my previous command
gave practically all processes
0
 
omarfaridCommented:
try this

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

you may change -20 to any value you need
0
 
upanwarCommented:
I have tested below given script.

#!/bin/bash
while :
do

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

sleep 3

done

It will run in infinite loop and provide output for top 20 CPU intensive processes after every three second.

Just create a file and paste the code in that file and give that file to execute permission and execute this script with nohup. It will work.

0
 
wesly_chenCommented:
#!/bin/sh
while
do
    ps aux | sed 1d | sort -k3,3 -nr | head -20 > top20_`date +Y%m%d_%H%M%s`
    sleep 3    # pause for 3 seconds
done
0
 
wesly_chenCommented:
Or if you perfer to use top instead of ps
--------
#!/bin/sh
while
do
    top -cSb n 1 | tail -n+9 | sort -k9 -nr > top20_`date +%Y%m%d-%H%M%S`
    sleep 3    # pause for 3 seconds
done

0
 
wesly_chenCommented:
Woo, forget the "head -20"
--------
#!/bin/sh
while
do
    top -cSb n 1 | tail -n+9 | sort -k9 -nr | head -20 > top20_`date +%Y%m%d-%H%M%S`
    sleep 3    # pause for 3 seconds
done
0
 
sunhuxAuthor 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 ] "
0
 
upanwarCommented:
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
0
 
wesly_chenCommented:
Good catch.

However, why do you need to loop for 25000 loops
-n : Number of iterations limit as: -n number
    Specifies the maximum number of iterations, or frames, top should produce before ending.
----------------------
#!/bin/bash
i=0
while [ $i -lt 25000 ]
do
    top -cSb n 1 | tail -n+9 | sort -k9 -nr | head -20 > top20_`date +%Y%m%d-%H%M%S`
    let i++
    sleep 3    # pause for 3 seconds
done
0
 
sunhuxAuthor Commented:
Ok
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now