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
Comments on this entry are closed.
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
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
@Madhan
This may be the one you are looking for…
top -n1 -b | sed -n ‘8,$p’ | sort -r -n -k 10,10
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
@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.
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?
Hi,
to see a saved top file try:
[root@xyz ~]# cat filename
Abhi.
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?
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?
Hi ,
I need top reading to be stored into a file in every 5 mins .
Please provide me the command .
Regards,
Manas
I tried to write the output to file and i got some thing like this.
[?25h[m[H[2J7[1;44r8System: ussbtps6[38CSat Jan 26 23:19:46 2013[BLoad averages: 4.65, 4.88, 4.02[B1024 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
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”
to get output of TOP command very helpful and useful.