LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   top command (https://www.linuxquestions.org/questions/programming-9/top-command-367994/)

Volcano 09-28-2005 11:05 PM

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

flower.Hercules 09-28-2005 11:23 PM

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.

debianmike 09-28-2005 11:33 PM

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

Volcano 09-29-2005 01:04 AM

>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 .

flower.Hercules 09-29-2005 08:33 AM

" command >file "

will overwrite

" command >>file "

will append

addy86 09-29-2005 10:13 AM

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).

flower.Hercules 09-29-2005 10:16 AM

One > is overwrite, two >> is append; append means add to the end.

Works fine on my system.

puffinman 09-29-2005 11:20 AM

This seems to work a little better...
Code:

perl -e 'while(1) {`top -b -n 1 > testfile`;sleep(1)}'

addy86 09-29-2005 01:16 PM

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.

sirclif 09-29-2005 03:40 PM

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.

Volcano 09-30-2005 12:45 AM

Code:

top -d 1 >~/.top_logfile
why u are using "~" the tilda ? any importance to that ?

Volcano 09-30-2005 01:58 AM

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 .

Volcano 09-30-2005 01:59 AM

how do i overwrite the file content in every 1 secs interval ? is it not possible in RED HAT 9 ?

puffinman 09-30-2005 06:55 AM

Try reading my reply!!

flower.Hercules 09-30-2005 07:54 AM

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


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