定时按天压缩Nginx日志并清理n天前的日志

本文介绍了一种使用shell脚本实现的方法,每日定时对Nginx日志进行压缩,并清理超过特定天数的日志文件。脚本会根据日期对日志进行归档,同时利用Nginx信号量进行日志重读,确保服务不受影响。通过crond设置为定时任务,确保自动化执行。
摘要由CSDN通过智能技术生成

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 "------------------------------------------
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值