LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 10-08-2009, 02:31 AM   #1
soltanloo
Member
 
Registered: Jun 2009
Posts: 39

Rep: Reputation: 15
a simple question about top command


Hi

i want to print top command's output into a text file. and i use this command in bach.
Code:
top -n 1 >> test.txt
but after opennig text file i see that text file contains bad chararcters.
Code:
(Btop - 14:24:21 up  1:22,  7 users,  load average: 1.20, 1.36, 1.25(B
Tasks:(B(B 167 (Btotal,(B(B   2 (Brunning,(B(B 153 (Bsleeping,(B(B  11 (Bstopped,(B(B   1 (Bzombie(B
Cpu(s):(B(B 32.2%(Bus,(B(B  1.1%(Bsy,(B(B  0.1%(Bni,(B(B 65.0%(Bid,(B(B  1.6%(Bwa,(B(B  0.0%(Bhi,(B(B  0.0%(Bsi,(B(B  0.0%(Bst(B
Mem: (B(B  2073772k (Btotal,(B(B  1190496k (Bused,(B(B   883276k (Bfree,(B(B    84124k (Bbuffers(B
Swap:(B(B  4128760k (Btotal,(B(B        0k (Bused,(B(B  4128760k (Bfree,(B(B   658780k (Bcached(B

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                            (B
(B(B 3955 root      20   0  2920  652  560 R 98.7  0.0  42:58.51 test                               (B
(B 2137 root      20   0  337m  56m 7868 S  5.9  2.8   4:05.86 Xorg                               (B
(B 4345 root      20   0 56764  23m  15m S  5.9  1.2   1:10.35 gnome-system-mo                    (B
(B 2807 root      20   0  264m 144m  21m S  2.0  7.1   3:15.06 firefox                            (B
(B 3043 root      20   0  106m  23m  12m S  2.0  1.1   0:08.75 gnome-terminal                     (B
(B    1 root      20   0  2008  772  564 S  0.0  0.0   0:00.99 init                               (B
(B    2 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 kthreadd                           (B
(B    3 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 migration/0                        (B
(B    4 root      15  -5     0    0    0 S  0.0  0.0   0:00.13 ksoftirqd/0                        (B
(B    5 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0                         (B
(B    6 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 migration/1                        (B
(B    7 root      15  -5     0    0    0 S  0.0  0.0   0:00.05 ksoftirqd/1                        (B
(B    8 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/1                         (B
(B    9 root      15  -5     0    0    0 S  0.0  0.0   0:00.03 events/0                           (B
(B   10 root      15  -5     0    0    0 S  0.0  0.0   0:00.01 events/1                           (B
(B   11 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 khelper                            (B
(B   85 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/0                      (B
(B   86 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/1                      (B
(B   88 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 kblockd/0                          (B
(B   89 root      15  -5     0    0    0 S  0.0  0.0   0:00.06 kblockd/1                          (B
(B   91 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 kacpid                             (B
(B   92 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 kacpi_notify                       (B
(B  185 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 cqueue                             (B
(B  189 root      15  -5     0    0    0 S  0.0  0.0   0:00.08 ata/0                              (B
(B  190 root      15  -5     0    0    0 S  0.0  0.0   0:00.44 ata/1                              (B
(B  191 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 ata_aux                            (B
(B  193 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 ksuspend_usbd                      (B
(B  198 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 khubd                              (B
(B  201 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 kseriod                            (B
(B  245 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pdflush                            (B
(B  246 root      20   0     0    0    0 S  0.0  0.0   0:00.20 pdflush                            (B
(B  247 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 kswapd0                            (B
(B  295 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 aio/0                              (B
(B  296 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 aio/1                              (B
(B  516 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 kpsmoused                          (B
(B  523 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 kstriped                           (B
(B  526 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 ksnapd                             (B
(B  559 root      15  -5     0    0    0 S  0.0  0.0   0:00.88 scsi_eh_0                          (B
(B  560 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_1                          (B
(B  569 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 kdmflush                           (B
(B  570 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 kdmflush                           (B
(B  571 root      15  -5     0    0    0 S  0.0  0.0   0:00.43 kjournald                          (B[?12l[?25h
how can i fix it ?
thank u in advance.
 
Old 10-08-2009, 02:39 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by soltanloo View Post
Hi

i want to print top command's output into a text file. and i use this command in bach.
Code:
top -n 1 >> test.txt
but after opennig text file i see that text file contains bad chararcters.

[ snip ]

how can i fix it ?
thank u in advance.
Those "extra" characters are position commands using the ANSI coding scheme:

http://en.wikipedia.org/wiki/ANSI_escape_code

All you need to do is write an interpreter to convert all the position commands into actual positions in a text buffer, than save the text buffer.

For mortals with finite lifetimes, it seems that the "top" output isn't meant to be saved as a straight text file.
 
Old 10-08-2009, 03:04 AM   #3
soltanloo
Member
 
Registered: Jun 2009
Posts: 39

Original Poster
Rep: Reputation: 15
thank u my friend.



i think there is another simple command instead of top.
i need to read cpu utilization. i know about mpstat or iostat command. but these commands shows the average values and i need the real time value. do know another command for reading cpu utilization ?

Last edited by soltanloo; 10-08-2009 at 04:47 AM.
 
Old 10-08-2009, 05:32 AM   #4
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by soltanloo View Post
thank u my friend.

i think there is another simple command instead of top.
i need to read cpu utilization. i know about mpstat or iostat command. but these commands shows the average values and i need the real time value. do know another command for reading cpu utilization ?
Capture the output of --

Code:
$ ps aux
-- and sum the values in column (2) (zero-based) for an estimate of CPU utilization.

Like this:

Code:
total=0

while read line
do
    array=($line)
    v=${array[2]}
    if [[ $v =~ [0-9] ]]
    then
	total=$(echo "scale=2; $total + $v" | bc)
    fi
done <<__EOF__
    $(ps aux)
__EOF__

echo "CPU USage: $total%"
There are some odd-looking methods used in this script. I assure you, they are necessary. To see for yourself, try writing it another way.
 
Old 10-08-2009, 06:21 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 680Reputation: 680Reputation: 680Reputation: 680Reputation: 680Reputation: 680
You could use the -b option for top to prevent the special charactors from being produced in the first place:
top -b -d 10 -n 5 >top.log
 
Old 10-08-2009, 08:48 AM   #6
soltanloo
Member
 
Registered: Jun 2009
Posts: 39

Original Poster
Rep: Reputation: 15
thank u jschiwal and lutusp
they work. also i used mpstat 1 1 and it works too.
mpstat 1 1
thank u very much.
 
  


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
Question about using TOP command eyeh8flaherty Linux - Newbie 2 03-06-2009 11:09 AM
simple ln command question khaos83 Linux - Newbie 1 01-31-2008 02:54 AM
top command question mokku Linux - Newbie 3 11-21-2007 11:04 AM
simple cp command question basher400 Linux - Newbie 7 06-08-2005 08:26 AM
A shell command simple question the_rhino Linux - Newbie 6 09-13-2004 01:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:25 AM.

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
Open Source Consulting | Domain Registration