下面是脚本的一些参数说明,红色部分对应修改成自己需要的查询内容就可以了:
#!/bin/sh
logpath="$HOME/toplogs";#指定输出日志的目录
sleeptime=5; #执行top的间隔时间
headline=" PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND ";
iter=0; #迭代器初始化
maxiter=5; #设置多少行输出一次对应的headline的内容
while [ 1 ];do
logdate=`date +%Y-%m-%d`;
oldlogfile="$logfile";
logfile=${logpath}/top${logdate}.log;
if [ "$oldlogfile" != "$logfile" -o $iter -eq "$maxiter" ];then
echo "$headline" >> $logfile;
iter=0;
fi;
iter=`expr $iter + 1`;
synctime=`date '+DATE:%Y-%m-%d %H:%M:%S'`;
echo -n $synctime '>>' >> $logfile;
/usr/bin/top -b -n 1 | grep init | grep -v grep | cut -c1-96 2>&1 >> $logfile;
sleep $sleeptime;
done
输出信息如下:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
DATE:2009-03-13 11:54:32 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:54:38 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:54:43 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:54:49 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:54:54 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
DATE:2009-03-13 11:55:00 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:05 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:11 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:16 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:22 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
DATE:2009-03-13 11:55:27 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:33 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:38 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:44 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:49 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
DATE:2009-03-13 11:55:55 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:56:00 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:56:06 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:56:11 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:56:17 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:54:32 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:54:38 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:54:43 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:54:49 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:54:54 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
DATE:2009-03-13 11:55:00 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:05 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:11 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:16 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:22 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
DATE:2009-03-13 11:55:27 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:33 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:38 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:44 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:55:49 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
DATE:2009-03-13 11:55:55 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:56:00 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:56:06 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:56:11 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init
DATE:2009-03-13 11:56:17 >> 1 root 16 0 2404 592 504 S 0 0.0 0:02.27 init