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

by Ramesh Natarajan on October 23, 2009

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
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

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

  1. How To Add Timestamp to Unix Vmstat Command Output
  2. Can You Top This? 15 Practical Linux Top Command Examples
  3. Unix Stat Command: How To Identify File Attributes
  4. Unix bc Command Line Calculator in Batch Mode
  5. How To Install, Edit, or Remove Cron Jobs in Batch Mode
  

Vim 101 Hacks Book

{ 6 comments… read them below or add one }

1 Madhan October 25, 2009 at 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

2 nardi October 25, 2009 at 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

3 SathiyaMoorthy October 26, 2009 at 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

4 Madhan November 1, 2009 at 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

5 nardi November 2, 2009 at 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.

6 Raghavendra June 17, 2010 at 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?

Leave a Comment

Previous post:

Next post: