运维开发网

linux配置nginx定时日志分割

运维开发网 https://www.qedev.com 2020-10-17 12:10 出处:51CTO 作者:love8度幸福
脚本内容:cat/app/cut_ngx_log.sh#!/bin/sh#Program:#Autocutnginxlogscript.#nginx日志路径LOGS_PATH=/app/nginx/logsTODAY=$(date-d'today'+%Y-%m-%d)#移动日志并改名mv${LOGS_PATH}/error.log${LOGS_PATH}/error_${TODAY}.logmv$

脚本内容:

cat /app/cut_ngx_log.sh
#!/bin/sh
# Program:
#     Auto cut nginx log script.

# nginx日志路径 
LOGS_PATH=/app/nginx/logs
TODAY=$(date -d 'today' +%Y-%m-%d)

# 移动日志并改名
mv ${LOGS_PATH}/error.log ${LOGS_PATH}/error_${TODAY}.log
mv ${LOGS_PATH}/access.log ${LOGS_PATH}/access_${TODAY}.log

# 向nginx主进程发送重新打开日志文件的信号
kill -USR1 $(cat /app/nginx/logs/nginx.pid)

#自动删除30天前的指定日志文件
find ${LOGS_PATH}/ -mtime +30 -name "error*.log" -exec rm -rf {} \;
find ${LOGS_PATH}/ -mtime +30 -name "access*.log" -exec rm -rf {} \;

授权并添加定时任务

chmod +x /app/cut_ngx_log.sh
echo '59 23 * * * sh /app/cut_ngx_log.sh >> /app/nginx/logs/cutnginxlog.log 2>&1' >> /etc/crontab

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号