![](http://www.atcpu.com/themes/extres/ithread/images/7.gif) | Linux系统的日志文件,比如Apache的日志文件,在使用了一段时间后,可能会变得比较大,如果不清空的话,可能会发生这样那样的问题。定期清空一下这些日志文件是比较好的办法。 这里以清空Apache的日志文件为例,我使用的是系统的默认配置,日志的文件在/var/log/httpd下面,访问日志的格式是: CustomLog logs/access_log combined 生成的访问日志文件类似于:access_log_20101118……,可以使用shell脚本定期删除日志文件,只保留近3天的文件,以免日志文件占满磁盘空间。 建立清除日志文件的shell脚本,文件名为clean_log,存放目录任意 #! /bin/bash logdir=/var/log/httpd cd ${logdir} declare -i filesum=`ls access_log_* | wc -l` declare -i delnum=$filesum-3 if [ "${delnum}" -ge 1 ];then rm -rf `ls -tr access_log_* | head -${delnum}` fi 添加运行权限:chmod 755 clean_log 再添加为自动运行任务:vi /etc/crontab,添加: 01 04 * * * /root/Desktop/clean_log
| |