定义cut_nginx_log.sh
日志文件脚本如下
#!/bin/bash
#LOGS_PATH为日志存放路径
LOGS_PATH=/weblog/nginx/logs
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
KEEPTIME=$(date -d "-30 days" +%Y-%m-%d)
#切分日志文件
mv ${LOGS_PATH}/access.log ${LOGS_PATH}/access_${YESTERDAY}.log
mv ${LOGS_PATH}/error.log ${LOGS_PATH}/error_${YESTERDAY}.log
#通过Nginx信号量控制重读日志,/web/nginx/为nginx安装目录
kill -USR1 $(cat /web/nginx/logs/nginx.pid)
#删除30天前的日志文件
rm -f ${LOGS_PATH}/access_{KEEPTIME}.log
rm -f ${LOGS_PATH}/error_{KEEPTIME}.log
echo 0
如果sh脚本是从其他地方复制过来要转成unix格式:
yum install dos2unix -y
dos2unix 脚本.sh
添加系统定时任务
crontab -e #每天0时1分进行日志分割 01 00 * * * /web/nginx/logs/cut_nginx_log.sh #重启Linux定时任务 crond restart