利用shell脚本清理nginx日志
#!/bin/bash
set -e
# 设置日志文件存放目录
logs_path="/home/wwwlogs"
backup_path="/home/wwwlogs/archives"
# nginx pid
pid_number=$(ps -ef|grep nginx|grep master|head -1|awk '{print $2}')
if [ ! -e $backup_path ]; then
mkdir -p $backup_path
fi
# 检查日志数量
log_nums=$(ls $logs_path | grep access | wc -l)
if [ $log_nums -lt 1 ]; then
echo "nginx 日志为空"
exit 1
fi
# 移动旧日志
ls $logs_path | grep access | awk -F'.' '{print $1}' | xargs -I{} mv $logs_path/{}.log $backup_path/{}_$(date -d "yesterday" +"%Y%m%d").log
# 向nginx主进程发信号重新打开日志
kill -USR1 $pid_number
# 删除超过指定时间的日志文件,单位:天
find $backup_path -name "*.log" -type f -mtime +7 -exec rm -rf {} \;
转载https://blog.csdn.net/happy_teemo/article/details/109184236