- How to Capture top command output to a file
- Redirect the output of top command to a file
- Save top command output with more than one iteration
- How to capture the top command output to a file every 5 minutes for an hour
- How to capture the top command output to a file every 5 minutes for half an hour
- Closing Notes
- How Do I Redirect Top Output to a File in Linux?
- Method 1: Single File Output Redirection
- Method 2: Redirecting terminal output to a single file
- Method 3: The top command
- Conclusion:
- About the author
- Talha Saif Malik
- How To Capture Unix Top Command Output to a File in Readable Format
How to Capture top command output to a file
Linux top command is widely used by Linux system administrators in real time to check system resources utilization such as CPU, disk I/O, system load average, running processes and memory utilization.
I usually use Oracle OSWatcher Black Box (OSWbb) to collect various system data to diagnose performance issues for a period of time.
But if you want to collect a list of processes that consume high CPU and memory on your system for a specific period of time, you can do this using the top command.
To redirect the top command output to a text file, the top command must be executed in batch mode.
In this guide, we will show you how to capture the top command output in files for a specific duration for troubleshooting performance issues.
Redirect the output of top command to a file
Since I did not use the delay option, the below top command will redirect the output of the top command to a single iteration to a file.
top -b -n 1 > /home/linuxgeek/backup/top_output.txt
View the output from a file:
cat backup/top_output.txt | head -20 top - 14:11:15 up 2:34, 2 users, load average: 0.87, 0.98, 1.40 Tasks: 332 total, 2 running, 330 sleeping, 0 stopped, 0 zombie %Cpu(s): 3.3 us, 2.0 sy, 0.0 ni, 94.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st MiB Mem : 15831.25+total, 3907.852 free, 5055.945 used, 6867.457 buff/cache MiB Swap: 2048.332 total, 2048.332 free, 0.000 used. 8452.277 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 12091 linuxge+ 20 0 4210036 410464 204728 S 11.76 2.532 11:46.37 gnome-shell 14922 linuxge+ 20 0 3039312 350928 121348 S 11.76 2.165 2:10.17 Web Content 20076 linuxge+ 20 0 43696 4316 3692 R 11.76 0.027 0:00.04 top 12075 linuxge+ 9 -11 2802264 15704 11212 S 5.882 0.097 5:42.77 pulseaudio 14163 linuxge+ 20 0 3184192 524672 125504 S 5.882 3.236 3:31.18 Web Content 17274 linuxge+ 20 0 28.615g 324396 115180 S 5.882 2.001 27:47.93 chrome 1 root 20 0 238100 13744 10228 S 0.000 0.085 0:08.58 systemd 2 root 20 0 0 0 0 S 0.000 0.000 0:00.01 kthreadd 3 root 0 -20 0 0 0 I 0.000 0.000 0:00.00 rcu_gp 4 root 0 -20 0 0 0 I 0.000 0.000 0:00.00 rcu_par_gp 6 root 0 -20 0 0 0 I 0.000 0.000 0:00.00 kworker/0:0H-events_highpri 7 root 20 0 0 0 0 I 0.000 0.000 0:03.78 kworker/0:1-mm_percpu_wq 9 root 0 -20 0 0 0 I 0.000 0.000 0:00.00 mm_percpu_wq
Save top command output with more than one iteration
Run the below top command if you want to capture more than 1 iterations to a file. Lets say 5 iteration, then you will be able to see 5 output concatenated one after another in a log file.
Normally it runs continuously with a delay of 2 sec and you can change this value by adding ‘-d’ value in the top command as shown below:
top -n 5 -d 4 -b >/backup/top_output_1.txt
View the output from a file:
cat /backup/top_output_1.txt
How to capture the top command output to a file every 5 minutes for an hour
The below top command format capture the top command output to a file every 5 minutes for an hour. It creates a separate file every 5 minutes that helps you to identify a list of process that consumed more CPU and Memory on the system.
To do so, add the following cronjob:
The below cronjob will run from 5AM to 6AM every 5 minutes for an hour.
0-59/5 5 * * * top -n 10 -d 4 -b > /home/linuxgeek/backup/top_out-`date +\%Y\%m\%d\%H\%M\%S`.txt
- -n : Specifies the maximum number of iterations
- -b : Batch-mode operation (Starts top command in Batch mode to redirect the output to other programs or to a file)
- -d : Delay-time interval (Specifies the delay between screen updates)
ls -lh | head -13 total 26M -rw-r–r– 1 root root 116K Oct 25 05:00 top_out-20211025050001.txt -rw-r–r– 1 root root 129K Oct 25 05:05 top_out-20211025050501.txt -rw-r–r– 1 root root 124K Oct 25 05:10 top_out-20211025051001.txt -rw-r–r– 1 root root 122K Oct 25 05:15 top_out-20211025051501.txt -rw-r–r– 1 root root 119K Oct 25 05:20 top_out-20211025052001.txt -rw-r–r– 1 root root 118K Oct 25 05:25 top_out-20211025052501.txt -rw-r–r– 1 root root 119K Oct 25 05:30 top_out-20211025053001.txt -rw-r–r– 1 root root 116K Oct 25 05:35 top_out-20211025053501.txt -rw-r–r– 1 root root 115K Oct 25 05:40 top_out-20211025054001.txt -rw-r–r– 1 root root 116K Oct 25 05:45 top_out-20211025054501.txt -rw-r–r– 1 root root 115K Oct 25 05:50 top_out-20211025055001.txt -rw-r–r– 1 root root 111K Oct 25 05:55 top_out-20211025055501.txt
If you would like to capture the output in a single file, use the following cronjob: The below cronjob will run from 9AM to 10AM every 5 minutes for an hour and append the output in a same file.
0-59/5 9 * * * top -b -n5 -d 5 >>/home/linuxgeek/backup/top_output.txt
How to capture the top command output to a file every 5 minutes for half an hour
To do so, add the following cron job: The below cronjob runs every 5 minutes from 1.30PM to 2PM for half an hour.
30-59/5 13 * * * top -n 10 -d 4 -b > /home/linuxgeek/backup/top_out-`date +\%Y\%m\%d\%H\%M\%S`.txt
Closing Notes
In this guide, we’ve shown you how to capture top command output in a file using various formats in Linux.
If you have any questions or feedback, feel free to comment below.
How Do I Redirect Top Output to a File in Linux?
When a Linux user types any command into the bash prompt, the terminal usually prints the output of the invoked command so you can read it straight away. However, bash also permits you to “redirect” or save any command’s output in the system.
This article will discuss three different procedures of redirecting the output of the top command to any file.
Method 1: Single File Output Redirection
For utilizing the redirection of bash, execute any script, then define the > or >> operator followed by the file path to which the output should be redirected.
- “>>” operator is used for utilizing the command’s output to a file, including the output to the file’s current contents.
- “>” operator is used to redirect the command’s output to a single file and replace the file’s current content.
We can say that technically, this is a file redirection of “stdout,” which is the normal display. Now, we will execute the sample example. The “ls” command displays the content of the current directory’s folders and files after its execution.
However, this command will save the output to the specified file in the following example rather than printing it to the terminal.
Utilize the given command syntax for checking the content of the file.
Now, write out the below-given command for printing the content of the “output file” in the terminal.
The operator “>” overwrites the file content with the command execution output. Instead, you can use the “>>” operator for saving the multiple commands output in a single file. For instance, the execution of the given command will add the system information to the specific file.
$ uname -a >> / home / linuxhint / outputfile
$ cat / home / linuxhint / outputfile
Method 2: Redirecting terminal output to a single file
Didn’t like the idea of using the”>” or “>>” operator for redirecting output? Don’t worry! The tee command is here to rescue you.
The below-given tee command will overwrite the file content with the command’s output similar to the “>” operator.
Method 3: The top command
System administrators also use the Linux top command to view real-time system statistics such as load average, system uptime, running tasks, used memory, specific information about each running process, and a summary of threads or processes. By utilizing the -b flag, this command helps to get the information about the currently executing processes in the system. The top command will permit the top to function in batch mode and the -n flag to determine the number of iterations the command should take as output.
All of the output resulting from the top command’s execution will be redirected to the specified file. Now, write out the “less” command for checking the content of the file.
The -n flag will send the single snapshot of executed command to the specified file. To retrieve only the first iteration, specify the “1” after the “-n” flag.
Utilize the “cat” command for viewing the running tasks information.
Conclusion:
In Linux, for redirecting output to a file, utilize the ”>” and ”>>” redirection operators or the top command. Redirection allows you to save or redirect the output of a command in another file on your system. You can use it to save the outputs and use them later for different purposes.
About the author
Talha Saif Malik
Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.
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
-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