How to redirect the output of the top command to a file

To monitor the purpose of the system, I need to redirect the output of the top command to a file so that I use / analyze it. I am trying to do the same, but CPU performance statistics are not saved in the file. screenshots. Expected Result:

[ root@v100 /usr/local/bin]# top last pid: 6959; load averages: 0.01, 0.03, 0.03 up 0+02:47:34 17:51:16 114 processes: 1 running, 108 sleeping, 5 zombie CPU: 0.0% user, 0.0% nice, 1.6% system, 0.0% interrupt, 98.4% idle Mem: 734M Active, 515M Inact, 226M Wired, 212M Buf, 491M Free Swap: 4095M Total, 4095M Free PID USERNAME THR PRI NICE SIZE RES STATE TIME WCPU COMMAND 1953 root 150 20 0 3084M 635M uwait 2:44 0.00% java 1663 mysql 46 20 0 400M 139M sbwait 1:29 0.00% mysqld 1354 root 31 20 0 94020K 50796K uwait 0:24 0.00% beam 4233 root 1 20 0 122M 23940K select 0:06 0.00% python 1700 zabbix 1 20 0 20096K 2436K nanslp 0:03 0.00% zabbix_agentd 1799 zabbix 1 20 0 103M 7240K nanslp 0:02 0.00% zabbix_server 4222 root 1 30 0 122M 23300K select 0:02 0.00% python 1696 zabbix 1 20 0 19968K 2424K nanslp 0:02 0.00% zabbix_agentd 2853 root 1 20 0 126M 29780K select 0:02 0.00% python 1793 zabbix 1 20 0 103M 7152K nanslp 0:01 0.00% zabbix_server 1797 zabbix 1 20 0 103M 8348K nanslp 0:01 0.00% zabbix_server 1752 root 1 20 0 122M 22344K select 0:01 0.00% python 1796 zabbix 1 20 0 103M 8136K nanslp 0:01 0.00% zabbix_server 1795 zabbix 1 20 0 103M 8208K nanslp 0:01 0.00% zabbix_server 1801 zabbix 1 20 0 103M 7100K nanslp 0:01 0.00% zabbix_server 3392 root 1 20 0 122M 23392K select 0:01 0.00% python 1798 zabbix 1 20 0 103M 7860K nanslp 0:01 0.00% zabbix_server 2812 root 1 20 0 134M 25184K select 0:01 0.00% python 1791 zabbix 1 20 0 103M 7188K nanslp 0:01 0.00% zabbix_server 1827 root 1 -52 r0 14368K 1400K nanslp 0:01 0.00% watchdogd 1790 zabbix 1 20 0 103M 7164K nanslp 0:01 0.00% zabbix_server 1778 zabbix 1 20 0 103M 8608K nanslp 0:01 0.00% zabbix_server 1780 zabbix 1 20 0 103M 8608K nanslp 0:01 0.00% zabbix_server 2928 root 1 20 0 122M 23272K select 0:01 0.00% python 2960 root 1 20 0 116M 22288K select 0:01 0.00% python 1776 zabbix 1 20 0 103M 7248K nanslp 0:01 0.00% zabbix_server 2892 root 1 20 0 122M 22648K select 0:01 0.00% python 1789 zabbix 1 20 0 103M 7128K nanslp 0:01 0.00% zabbix_server 1814 root 1 20 0 216M 15796K select 0:01 0.00% httpd 1779 zabbix 1 20 0 103M 8608K nanslp 0:01 0.00% zabbix_server 1783 zabbix 1 20 0 103M 8608K nanslp 0:01 0.00% zabbix_server 1800 zabbix 1 20 0 103M 7124K nanslp 0:01 0.00% zabbix_server 1782 zabbix 1 20 0 103M 8608K nanslp 0:01 0.00% zabbix_server 1781 zabbix 1 20 0 103M 8608K nanslp 0:00 0.00% zabbix_server 1792 zabbix 1 20 0 103M 7172K nanslp 0:00 0.00% zabbix_server 2259 root 2 20 0 48088K 4112K uwait 0:00 0.00% cb_heuristics 

If I do:

 [ root@v100 /usr/local/bin]# top > /tmp/top.output 

then it shows:

 [ root@v100 /usr/local/bin]# cat /tmp/top.output last pid: 7080; load averages: 0.09, 0.06, 0.03 up 0+02:52:24 17:56:06 114 processes: 1 running, 108 sleeping, 5 zombie Mem: 731M Active, 515M Inact, 219M Wired, 212M Buf, 501M Free Swap: 4095M Total, 4095M Free PID USERNAME THR PRI NICE SIZE RES STATE TIME WCPU COMMAND 1953 root 150 20 0 3084M 633M uwait 2:17 0.00% java 1663 mysql 46 20 0 400M 136M sbwait 1:08 0.00% mysqld 1354 root 31 20 0 94020K 49924K uwait 0:18 0.00% beam 4233 root 1 20 0 122M 23776K select 0:04 0.00% python 1700 zabbix 1 20 0 20096K 2436K nanslp 0:02 0.00% zabbix_agentd 1799 zabbix 1 20 0 103M 7240K nanslp 0:01 0.00% zabbix_server 2853 root 1 20 0 126M 29780K select 0:01 0.00% python 1696 zabbix 1 20 0 19968K 2424K nanslp 0:01 0.00% zabbix_agentd 4222 root 1 28 0 122M 23264K select 0:01 0.00% python 1793 zabbix 1 20 0 103M 7152K nanslp 0:01 0.00% zabbix_server 1752 root 1 20 0 122M 22344K select 0:01 0.00% python 1797 zabbix 1 20 0 103M 8088K nanslp 0:01 0.00% zabbix_server 1796 zabbix 1 20 0 103M 7944K nanslp 0:01 0.00% zabbix_server 1795 zabbix 1 20 0 103M 8044K nanslp 0:01 0.00% zabbix_server 1801 zabbix 1 20 0 103M 7100K nanslp 0:01 0.00% zabbix_server 3392 root 1 20 0 122M 23312K select 0:01 0.00% python 2812 root 1 20 0 134M 25184K select 0:01 0.00% python 1798 zabbix 1 20 0 103M 7628K nanslp 0:01 0.00% zabbix_server 

so here I can control the memory, but not the processor, the reason is the output of the top redirect. CPU statistics not updated

How can I capture CPU statistics?

If you have any suggestions, tell me.

+6
source share
1 answer

top -b -n 1 seems to work on my Linux box here (-b: batch mode, -n: number of iterations).

Edit:

I just tried this on FreeBSD 9.2, which uses the 3.5beta12 version of top . It seems that at least one additional iteration is required to get processor statistics. Therefore you can use:

 top -b -d2 -s1 | sed -e '1,/USERNAME/d' | sed -e '1,/^$/d' 

-b: batch mode, -d2: 2 (the first does not contain CPU statistics, the second does), -s1: wait one second between displays

The sed pipeline deletes the first screen that does not contain CPU statistics (by skipping the header and process list).

+10
source

Source: https://habr.com/ru/post/958554/


All Articles