相信接触过服务器维护或者应用管理的同学都有被大量的服务器日志困扰过吧,至少我是这样,废话少说~直接上脚本,非常简单
#!/bin/bash
PARAM_SIZE=$#
if [[ $PARAM_SIZE -lt 2 ]]; then
echo "usage: $0 <log_dir> <pattern> [days]"
echo " log_dir: the log directory"
echo " pattern: match the log to delete"
echo " days: n+1 days ago; defaut 0, before today's log."
echo " $0 /var/log/nginx/access.log.* 3 : delete nginx's access log 4 days ago."
fi
LOG_DIR="$1"
LOG_PATTERN="$2"
if [[ $PARAM_SIZE -gt 2 ]]; then
DAYS=$3
else
DAYS=0
fi
find $LOG_DIR -mtime +${DAYS} -name "$LOG_PATTERN" -exec rm -rvf {} \;
执行:./dellog.sh /var/log/nginx/access.log.* 1
可以结合crontab使用,自动清理日志就完成了,真是一劳永逸的办法