nginx 日志切割shell脚本

本文介绍了一个用于Nginx服务器的日志文件自动切割脚本,该脚本使用bash语言编写,能够按日期自动将当前日志文件重命名为历史文件,并重新启动Nginx服务确保新的日志文件开始记录。此过程包括检查Nginx安装目录的存在性、确认日志文件的存在并执行日志文件的重命名操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/bin/bash 
dateformat=`date +%Y%m%d`
date_format=`date`
basedir="/usr/local/nginx"
nginxlogdir="$basedir/logs"
log_access="access"
log_error="error"
[ -d $basedir ] && cd $nginxlogdir || exit 1 
[ -f ${log_access}.log ] && [ -f ${log_error}.log ] || exit 1
/bin/mv ${log_access}.log ${log_access}_${dateformat}.log
/bin/mv ${log_error}.log ${log_error}_${dateformat}.log 
$basedir/sbin/nginx -s reload
sleep 1s
if [ ! -f ${nginxlogdir}/nginx.pid ];then
     $basedir/sbin/nginx -s reload
     echo "$date_format nginx 第二次重启成功" >> split.logs
     exit 0
else 
    echo "$date_format nginx 第一次重启成功" >> split.logs 

    exit 0

fi

如有不正确之处,请多多指教!本人小白



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值