首页 > 代码库 > Nginx 定时切换日志(shell)

Nginx 定时切换日志(shell)

1.clearNginxLog.sh

 

#!/bin/bash 

#run at 2:00am everyday

nginx_pid="/run/nginx.pid"

logs_path="/opt/log/nginx"

logs_date="$(date -d "yesterday" +"%Y%m%d")_`date +%Y%m%d%H%M%S`"

mv ${logs_path}/access.log ${logs_path}/${logs_date}_access.log

#mv ${logs_path}/error.log ${logs_path}/${logs_date}_error.log

kill -USR1 `cat ${nginx_pid}`

rm ${logs_date}_access.log

 

2.添加定时任务

vim /etc/crontab

00 2    * * *   root    /opt/log/nginx/clearNginxLog.sh

 

其中,clearNginxLog.sh里面的内容可自己定义,以上是直接删除,可备份或压缩

Nginx 定时切换日志(shell)