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

Details:

  • -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)

Output:

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

Output:

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.

Leave a Reply

Your email address will not be published.