今天写了一个脚本,用以分割日志,记录一下
#!/bin/bash
#修改变量值为真实安装地址
NGINX_DIR=/usr/local/nginx/
mkdir -p $NGINX_DIR/logs/split
#复制原文件
cp $NGINX_DIR/logs/error.log $NGINX_DIR/logs/split/error-$(date -d "yesterday" +"%Y%m%d").log
cp $NGINX_DIR/logs/access.log $NGINX_DIR/logs/split/access-$(date -d "yesterday" +"%Y%m%d").log
#清空日志内容
cat /dev/null > $NGINX_DIR/log/error.log
cat /dev/null > $NGINX_DIR/log/access.log
#定期清理日志
#保留多少天就把参数写多少天
CLEAR_DATA=180
find $NGINX_DIR/logs/split/error -mtime +$CLEAR_DATA -type f -name \*.log | xargs rm -f
find $NGINX_DIR/logs/split/access -mtime +$CLEAR_DATA -type f -name \*.log | xargs rm -f
然后给shell赋权777,再到corntab里面做一下计划任务,每天0点定时运行
[root@host nginx]# crontab -u root -e
[root@host nginx]# crontab -u root -l
0 0 * * * /usr/local/nginx/clear_log.sh