nginx日志定时切割脚本
时间:2010-07-19 来源:hillside
1 编写脚本cut_nginx_log.sh,放置在nginx/sbin/目录下,修改成可执行
#!/bin/bash
# This script run at 00:00
#放置日志目录
logs_path="/var/www/logs"
#nginx 目录
nginx_path="/var/www/nginx"
#创建日期目录
mkdir -p ${logs_path}/$(date -d "yesterday" +"%Y.%m")/
#移动nginx/logs/access.log日志到指定目录
mv ${nginx_path}/logs/access.log ${logs_path}/$(date -d "yesterday" +"%Y.%m")/$(date -d "yesterday" +"%Y.%m.%d").log
#通知nginx进程刷新日志文件
kill -USR1 `cat ${nginx_path}/logs/nginx.pid`
2 添加系统定时任务:sudo crontab -e
00 00 * * * /var/www/nginx/sbin/cut_nginx_log.sh
#!/bin/bash
# This script run at 00:00
#放置日志目录
logs_path="/var/www/logs"
#nginx 目录
nginx_path="/var/www/nginx"
#创建日期目录
mkdir -p ${logs_path}/$(date -d "yesterday" +"%Y.%m")/
#移动nginx/logs/access.log日志到指定目录
mv ${nginx_path}/logs/access.log ${logs_path}/$(date -d "yesterday" +"%Y.%m")/$(date -d "yesterday" +"%Y.%m.%d").log
#通知nginx进程刷新日志文件
kill -USR1 `cat ${nginx_path}/logs/nginx.pid`
2 添加系统定时任务:sudo crontab -e
00 00 * * * /var/www/nginx/sbin/cut_nginx_log.sh
相关阅读 更多 +