Nginx日志每天定时压缩并清理n天前的日志
以下是shell脚本,脚本执行也会生成一个执行日志,路径自行更改。
脚本主要将指定日志目录的所有*.log文件按天压缩,并通过Nginx信号量控制重读日志
(kill -USR1 cat **/nginx.pid
),最后删除n天以前的日志压缩文件
用crond添加定时任务
##不同用户有各自的定时任务
crontab -e
## 每天定时凌晨1点执行脚本
0 1 * * * sh (绝对路径)/cutNginxLog.sh
#!/bin/bash
##function: cut nginx log files
## set the log path for this shell Script
cutNginxLog_path="/applog/cutNginxLog"
cutNginxLog_log="/applog/cutNginxLog/cutNginxLog.log"
## set nginx log path and nginx pid path
nginx_log_path="/applog/nginx"
nginx_log_pid="/applog/nginx/nginx.pid"
nginx_log_dir=${nginx_log_path}
## set how long you want to save
save_days=7
###############################################
## Please do not modify the following Script ##
###############################################
## create the log path for this Script
### -x
if [ ! -x "$cutNginxLog_path" ];then
mkdir "$cutNginxLog_path"
fi
### -d
if [ ! -d "$cutNginxLog_path" ];then
mkdir "$cutNginxLog_path"
fi
### -f
if [ ! -d "$cutNginxLog_log" ];then
touch "$cutNginxLog_log"
fi
echo "------------------------------------------