crontab里shell脚本将top信息写入文件:
注:
1、top -n 1代表执行1次退出(默认top是不退出的),-d 1代表每1秒执行1次
2、crontab里需加/bin/bash
# crontab -e
*/5 * * * * /bin/bash /usr/local/bin/top.sh
# vi top.sh
#!/bin/bash
Date=`date +%Y%m%d`
Date2=`date +%Y%m%d_%H%M`
Date3=`date +%Y%m%d -d "7 days ago"`
echo "$Date2" >> /log/Top/$Date.log
/usr/bin/top -b -d 1 -n 1 | head -n 15 >> /log/Top/$Date.log
echo " " >> /log/Top/$Date.log
echo " " >> /log/Top/$Date.log
/bin/rm -rf /log/Top/$Date3.log
:wq