≡ Menu

How To Capture Unix Top Command Output to a File in Readable Format

Question: I’m trying to capture the output of the top command into a file. When I execute top > output.txt, the output.txt file contains lot of junk characters. What is the best method to capture the output of the top command into a readable text file?

Answer: Use the top command batch mode operation option ( -b ) to capture the top command output into a file.

If you try to redirect the top command output to a text file as shown below, you’ll notice that the output file contains lot of junk characters.

When you try to view the output file using less command, you’ll notice that the output file is created with lot of junk characters.

$ top -n 1 > top-output.txt

$ less top-output.txt
"top-output.txt" may be a binary file.  See it anyway? 

Note: Option -n 1 indicates that only one iteration of the top command should be executed.

To avoid this problem and get a readable top command output, use option -b in the top command. Execute top command in batch mode as shown below.

$ top -n 1 -b > top-output.txt

$ less top-output.txt
top - 16:56:36 up 246 days, 11:14,  3 users,  load average: 0.00, 0.00, 0.00
Tasks: 168 total,   1 running, 167 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   8306856k total,  7940744k used,   366112k free,   285136k buffers
Swap:  8385920k total,      104k used,  8385816k free,  7391824k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
    1 root      15   0  2064  592  512 S  0.0  0.0   0:02.24 init               
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.47 migration/0        
    3 root      35  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0        
    4 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0         
    5 root      RT  -5     0    0    0 S  0.0  0.0   0:00.61 migration/1       

About option -b and -n from the top command man page:

       -b : Batch mode operation
            Starts top in "Batch mode", which could be useful for sending out-
            put 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.

       -n : Number of iterations limit as:  -n number
            Specifies the maximum number of iterations, or frames, top  should
            produce before ending.

You can also use this method to redirect the output of top command to another program as shown below.

$ top -n1 -b | head
top - 16:58:36 up 246 days, 11:14,  3 users,  load average: 0.00, 0.00, 0.00
Tasks: 169 total,   1 running, 168 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   8306856k total,  7941612k used,   365244k free,   285144k buffers
Swap:  8385920k total,      104k used,  8385816k free,  7392088k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
    1 root      15   0  2064  592  512 S  0.0  0.0   0:02.24 init               
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.47 migration/0        
    3 root      39  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0        
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Madhan October 25, 2009, 11:34 am

    If i need a top output sorted by memory utilization by processes into a text file for every 15 mins. how can we do this

  • nardi October 25, 2009, 3:51 pm

    Madhan you could put this script to your cron or whatever.
    top -n1 -b | sed -n ‘8,$-1p’ | sort -r -n +9 -9 > /var/log/memusg.log

  • SathiyaMoorthy October 26, 2009, 9:41 am

    @Madhan
    This may be the one you are looking for…

    top -n1 -b | sed -n ‘8,$p’ | sort -r -n -k 10,10

  • Madhan November 1, 2009, 9:03 am

    Hi nardi,
    It is throwing an error as follows:
    [root@madhan ~]# top -n1 -b | sed -n ‘8,$-1p’ | sort -r -n +9 -9 > /var/log/memusg.log
    sed: -e expression #1, char 4: unknown command: `-‘

    sathiyaMoorthy : it is working.
    Thanks a lot to all

  • nardi November 2, 2009, 5:51 am

    @Madhan Sry. I was trying if sed can handle something like “last but one” notation and sent wrong “version” of command. SathiyaMoorthy corrected this mistake.

  • Raghavendra June 17, 2010, 8:12 am

    Hi,

    I have tried executing the Top command. I get the Junk characters in the file.
    I have tried the Less command given but i get a error “command not found” but when i try man less i get an output.

    I am doing this on HP-UNIX.
    Any ideas and suggestions?

  • abhi January 25, 2011, 7:59 am

    Hi,

    to see a saved top file try:
    [root@xyz ~]# cat filename

    Abhi.

  • Carlos April 19, 2012, 3:36 pm

    What if I want to save the output of the top command, but I want the cpu´s to be showed individually, like pressing “1”, when you are inside top command. Can that be possible?

  • Okko Willeboordse June 29, 2012, 12:52 pm

    When redirecting top output, top truncates the lines depending of what top thinks the width of the terminal is.
    This is very annoying since I need top to output the command line (-c) option.
    Anyone knows how to overcome that?

  • Manas October 11, 2012, 5:53 am

    Hi ,

    I need top reading to be stored into a file in every 5 mins .
    Please provide me the command .

    Regards,
    Manas

  • Ravi January 26, 2013, 11:08 pm

    I tried to write the output to file and i got some thing like this.

    [?25h78System: ussbtps6Sat Jan 26 23:19:46 2013Load averages: 4.65, 4.88, 4.021024 processes: 965 sleeping, 51 running,

    Can you please help me how i can get in below format.

    System: ussbtps6 Sun Jan 27 00:08:02 2013
    Load averages: 2.85, 2.83, 3.12
    1019 processes: 959 sleeping, 52 running, 8 zombies
    Cpu states:
    CPU LOAD USER NICE SYS IDLE BLOCK SWAIT INTR SSYS
    0 3.11 1.8% 0.0% 35.1% 63.1% 0.0% 0.0% 0.0% 0.0%
    1 2.81 1.0% 0.0% 76.9% 22.2% 0.0% 0.0% 0.0% 0.0%
    2 2.73 4.5% 0.0% 25.1% 70.4% 0.0% 0.0% 0.0% 0.0%
    3 2.75 3.9% 0.0% 24.9% 71.2% 0.0% 0.0% 0.0% 0.0%
    — —- —– —– —– —– —– —– —– —–
    avg 2.85 2.7% 0.0% 40.5% 56.8% 0.0% 0.0% 0.0% 0.0%

    Memory: 5439940K (4239016K) real, 12681076K (9269948K) virtual, 166600K free Page# 1/36

    CPU TTY PID USERNAME PRI NI SIZE RES STATE TIME %WCPU %CPU COMMAND
    1 ? 20819 root 148 20 2508K 576K run 51:32 90.55 90.39 fsadm
    3 ? 39 root 152 20 9632K 9632K run 2528:09 63.03 62.92 vxfsd
    2 ? 24230 cyclone 152 20 1147M 834M run 350:33 10.07 10.05 java
    1 ? 24231 cyclone 152 20 1164M 416M run 25:47 2.63 2.63 java
    2 ? 24086 cyclone 152 20 1403M 1135M run 120:23 2.48 2.48 java
    3 ? 15118 oracle 156 20 768M 4800K sleep 0:52 2.46 2.45 oraclecyclnx

  • alsange April 22, 2015, 8:48 am

    hi!
    a need to save tops results for 2 minute (separated somehow)
    can u teach me?

    this is what i try but it saves only one iteration result

    x=`top -b -n 120 | head 20`
    echo “$x”

  • MNS January 13, 2016, 10:20 am

    to get output of TOP command very helpful and useful.