Solved Is it possible to periodically capture "top" result

I want to write a script to record the CPU usage trend, and found only "top" can give me the number of CPU usage, but its result cannot be redirected to a file periodically.

Is there any suggestions?
 
I want to write a script to record the CPU usage trend, and found only "top" can give me the number of CPU usage, but its result cannot be redirected to a file periodically.
Yes, it can. See top(1):
Code:
       -b     Use "batch" mode.  In this mode, all input from the terminal  is
              ignored.  Interrupt characters (such as ^C and ^\) still have an
              effect.  This is the default on a dumb  terminal,  or  when  the
              output is not a terminal.

However, tools like vmstat(8) and iostat(8) are much more suited for this.
 
No, it can't. I'm assuming you didn't try it ;-)
You assumed wrong:
Code:
dice@molly:~ % mkdir tmp
dice@molly:~ % cd tmp/
dice@molly:~/tmp % ll
total 0
dice@molly:~/tmp % top -b > top.txt
dice@molly:~/tmp % cat top.txt 
last pid: 14425;  load averages:  0.08,  0.16,  0.20  up 4+23:53:06    13:33:04
69 processes:  1 running, 68 sleeping

Mem: 436M Active, 68M Inact, 4998M Wired, 592M Buf, 2328M Free
ARC: 2518M Total, 696M MFU, 1693M MRU, 320K Anon, 31M Header, 98M Other
Swap: 16G Total, 595M Used, 15G Free, 3% Inuse


  PID USERNAME      THR PRI NICE   SIZE    RES STATE   C   TIME    WCPU COMMAND
 2832 freenet       129  52    0  2112M   350M uwait   0 635:21   1.86% java
 2873 root            1  20    0 28152K  4088K select  0   6:25   0.10% tmux
  703 transmission    3  20    0   131M 17256K kqread  1  58:49   0.00% transmission-daemon
 2780 root            1  20    0 85056K  1700K select  2  33:15   0.00% smbd
 1313     88         23  31    0  2179M   107M select  3   4:20   0.00% mysqld
 2794 dice            1  20    0 82868K  1576K select  3   0:31   0.00% sshd
  672 root            1  20    0 14540K   968K select  2   0:05   0.00% mountd
  976 root            1  20    0 24048K   768K select  0   0:05   0.00% sendmail
 1157 root            1  20    0 24048K   792K select  2   0:05   0.00% sendmail
 1315 root            1  20    0 20432K  1224K select  3   0:04   0.00% sendmail
  718 root            1  20    0 59424K  1532K select  0   0:03   0.00% nmbd
  545 root            1  20    0 10448K   676K select  2   0:01   0.00% syslogd
 7960 dice            1  20    0 82868K  2032K select  1   0:01   0.00% sshd
  970 www             1  20    0 28600K  1100K kqread  2   0:01   0.00% nginx
 1322 root            1  49    0 12540K   672K nanslp  0   0:01   0.00% cron
 1164 root            1  20    0 16544K   572K nanslp  1   0:01   0.00% cron
  834 root            1  20    0 69488K  1676K select  0   0:01   0.00% smbd
 2792 root            1  20    0 82868K  1664K select  1   0:01   0.00% sshd
 
You guys are talking past each other, arguing the same side of the issue. SirDice was giving more details for your post, as simply saying "run top via cron" doesn't cover everything (missing -b, as SirDice mentioned in his post). :)
 
Thanks all. I found cron(8) wakes up every minute, but I need to check CPU usage every second. So top -b is the only choice currently. I have to write a script to invoke top -b every second and dump result to a specified file.
 
Back
Top