LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2005, 11:05 PM   #1
Volcano
Member
 
Registered: Sep 2005
Posts: 221

Rep: Reputation: 15
top command


i want to run top command every 1 second interval and want to store the output in a text file. so in every 1 second interval my text file should be overwritten by the new fresh output data.

my top command is :


top -d -1


Question is :

How do i store the output in a text file and how do they be overwritten by the fresh output data in every 1 secs interval ?



my system is red hat 9

Last edited by Volcano; 09-28-2005 at 11:08 PM.
 
Old 09-28-2005, 11:23 PM   #2
flower.Hercules
Member
 
Registered: Aug 2005
Distribution: Gentoo
Posts: 228

Rep: Reputation: 31
top -d 1 >~/.top_logfile

will output the contents of top once every second to the file you specify, in this case, .top_logfile in your home directory.
 
Old 09-28-2005, 11:33 PM   #3
debianmike
Member
 
Registered: Sep 2005
Posts: 115

Rep: Reputation: 15
isn't there a file that does this already? somewhere in /proc? you could just cp that file every 1 second or so
 
Old 09-29-2005, 01:04 AM   #4
Volcano
Member
 
Registered: Sep 2005
Posts: 221

Original Poster
Rep: Reputation: 15
>will output the contents of top once every second to the file you specify, in this >case, .top_logfile in your home directory.

will it overwrite everytime ? or the new results will be apppended ? i want the results should be overwritten everytime.


>isn't there a file that does this already? somewhere in /proc? you could just cp >that file every 1 second or so

i may change 1 seconds to 4 seconds later on. i may customize it . so, i dont want the default log file .
 
Old 09-29-2005, 08:33 AM   #5
flower.Hercules
Member
 
Registered: Aug 2005
Distribution: Gentoo
Posts: 228

Rep: Reputation: 31
" command >file "

will overwrite

" command >>file "

will append
 
Old 09-29-2005, 10:13 AM   #6
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
top -d 1 > logfile
does not do what he wants (doesn't even produce correct results), neither does
top -d 1 -b > logfile
since the new content is appended every second, nothing is overwritten (except for the content of logfile at the beginning).
 
Old 09-29-2005, 10:16 AM   #7
flower.Hercules
Member
 
Registered: Aug 2005
Distribution: Gentoo
Posts: 228

Rep: Reputation: 31
One > is overwrite, two >> is append; append means add to the end.

Works fine on my system.
 
Old 09-29-2005, 11:20 AM   #8
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
This seems to work a little better...
Code:
perl -e 'while(1) {`top -b -n 1 > testfile`;sleep(1)}'
 
Old 09-29-2005, 01:16 PM   #9
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Quote:
Originally posted by flower.Hercules
One > is overwrite, two >> is append; append means add to the end.

Works fine on my system.
I think what the original poster wanted is to overwrite the file every second (hence -d 1), not overwrite the file every time the command is executed.
 
Old 09-29-2005, 03:40 PM   #10
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
yea, > will overwrite every time you run the command, but by just using:

> top -d 1 > file.txt

top is continuing to run, so tile.txt is never closed and reopend for overwriting.
 
Old 09-30-2005, 12:45 AM   #11
Volcano
Member
 
Registered: Sep 2005
Posts: 221

Original Poster
Rep: Reputation: 15
Code:
top -d 1 >~/.top_logfile
why u are using "~" the tilda ? any importance to that ?
 
Old 09-30-2005, 01:58 AM   #12
Volcano
Member
 
Registered: Sep 2005
Posts: 221

Original Poster
Rep: Reputation: 15
hi,
it is not working ! it is not overwriting at all . its appending !
command was
top -d -1 >log.txt

i just need to get the fresh data every 1 secs interval from the log.txt file. so, i told it to be overwritten the content . i dont like append, if i append my file is going to be larger and t would be difficult to read the last status .

but surprisingly. i ran the above command and got my file appeneded to 1.4 MB !! . then i stopped the execution by CTRL+C .
 
Old 09-30-2005, 01:59 AM   #13
Volcano
Member
 
Registered: Sep 2005
Posts: 221

Original Poster
Rep: Reputation: 15
how do i overwrite the file content in every 1 secs interval ? is it not possible in RED HAT 9 ?
 
Old 09-30-2005, 06:55 AM   #14
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
Try reading my reply!!
 
Old 09-30-2005, 07:54 AM   #15
flower.Hercules
Member
 
Registered: Aug 2005
Distribution: Gentoo
Posts: 228

Rep: Reputation: 31
Yeah, puffinman's is the best answer in this group. The tilde specifies your home directory (~/) just for convenience.
 
  


Reply


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
command top getting error bluenectar Linux - General 2 08-19-2005 10:37 PM
Schedule TOP command in Crontab. imsajjadali Red Hat 2 05-20-2004 05:12 AM
top command, being hacked? shanenin Linux - Software 4 10-30-2003 09:25 PM
Results from top command...user Q...? cbjhawks Linux - Software 1 09-20-2003 11:06 PM
Where i can get the source code of the command 'top' hustning Programming 2 03-31-2002 01:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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