Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 03-30-2006, 12:02 PM   #1
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
Keeping track of process CPU %

Just wondered if anyone knows a way of logging top command for maybe an hour or so. I seem to get a process taking a high % of cpu, which I notice when playing fullscreen games that begin to stutter and slow down. This has only happened during the last few weeks and I suspect maybe fetchmail or a related process is the cause but would like to know for certain.
Old 03-30-2006, 02:04 PM   #2
Registered: Oct 2003
Location: New York
Distribution: Debian Sid
Posts: 185

Rep: Reputation: 30
From the top(1) man page:

-b : Batch mode operation
Starts top in 'Batch mode', which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you've set with the '-n' command-line option or until killed.
So just set the iteration number and delay time (with -n xxx -d yyy) and redirect the output to your logfile - voila!
Old 03-30-2006, 02:05 PM   #3
Registered: May 2004
Location: UK
Distribution: Debian, Ubantu, CentOS
Posts: 146

Rep: Reputation: 17

I hate to say something that I find annoying in my posts but...

If you look at the man page:
man top
it will say that you can use the -b switch for batch mode. this produces a nice output that you can pipe to annother application such as
top -b|grep cron
this allows you to watch an individual program

or you can output to a text file (using output redirect):
top -b>toplog.txt
or a combination of the two:
top -b|grep cron>toplog.txt
that answer the question?

p.s. if you didn't know about the man function or the pipe or redirecting of output then sorry about the RTFM
Old 03-30-2006, 02:09 PM   #4
Registered: Sep 2005
Distribution: Ubuntu
Posts: 252

Rep: Reputation: 30
Originally Posted by amon

or a combination of the two:
top -b|grep cron>toplog.txt
You may also want to use the '>>' to append to the file, instead of overwrite it.
Old 03-30-2006, 02:42 PM   #5
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Original Poster
Rep: Reputation: Disabled
Thx for the info, I did indeed try top -b | head and top -b > toplog but in the first case it stopped after 5 secs or so, and in the 2nd it didnt seem to be working as i expected. IE I could only get the processes at the moment I ctrl-c'd to end it.

Probably need to experiment with iterations.
Old 03-30-2006, 02:45 PM   #6
Senior Member
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
try running this
you can redirect it to a file or whatever and change intevals (seconds)
have to chmod 755 to make it runnable
oh yea the fourth collumn is recent CPU activity
while [ "$SECONDS" -le "$LIMIT" ]
    ps -ef | egrep -v "STIME|$LOGNAME" | sort +3 -r | head -n 15
    sleep $INTERVAL
exit 0


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Keeping track of logs mijohnst Linux - Software 1 03-29-2006 08:02 PM
Keeping track of my threads? une LQ Suggestions & Feedback 4 04-10-2005 10:35 PM
Volunteer needs help keeping track. mimsmall Linux - Software 1 10-10-2004 02:03 PM
keeping track of active desktops jalburger Linux - Newbie 0 07-02-2002 08:02 AM
Keeping track of packages NSKL Slackware 2 06-28-2002 10:23 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:49 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration