Link to home
Start Free TrialLog in
Avatar of sunhux
sunhux

asked on

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?
Avatar of upanwar
upanwar
Flag of India image

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
Avatar of sunhux
sunhux

ASKER


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

ASKER


& 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
for that you need to use nohup and & both.

# nohup <script.sh> &

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

ASKER



& I just need the top 20 CPU consuming processes but my previous command
gave practically all processes
ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sunhux

ASKER


 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 ] "
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sunhux

ASKER

Ok