主頁 > 知識庫 > nginx日志切割腳本分享

nginx日志切割腳本分享

熱門標(biāo)簽:打電話機(jī)器人接我是他的秘書 如果做線上地圖標(biāo)注 客服外呼系統(tǒng)怎么樣 揭陽智能電話機(jī)器人推薦 江蘇云電銷機(jī)器人公司 百度地圖標(biāo)注錯了有責(zé)任嗎 地圖標(biāo)注員都是年輕人 華鋒e路航港口地圖標(biāo)注 河南信譽好的不封卡電話外呼系統(tǒng)

實現(xiàn)方法一

#!/bin/bash
Logs_path="/data/Application/nginx/logs"
Pid_path="/data/Application/nginx/nginx.pid"
Month=`date +%Y-%m`
Date=`date +%Y-%m-%d`
Time=`date +%H`
WaitTime=$((24*60*60))

LogCut()
{
  cd $Logs_path
  mkdir -p $Month
  while true
  do
    mv $Logs_path/access.log $Logs_path/$Month/access_$Date.log
    kill -USR1 `cat ${Pid_path}`
  done
  sleep $WaitTime
  return 0
}

case $1 in
start)
    LogCut >/dev/null 
    ;;
stop)
    kill -9 `ps aux | grep LogCut | grep -v grep | awk '{print $2}'`>/dev/null
    ;;
restart)
    kill -9 `ps aux | grep LogCut | grep -v grep | awk '{print $2}'`>/dev/null
    LogCut >/dev/null 
    ;;
*)
    echo "Usage ERROR!Please use "start" or "stop" or "restart"!"
esac

實現(xiàn)方法二

試驗環(huán)境:

# cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 5.3 (Tikanga)

# /opt/nginx/nginx -v
nginx version: nginx/1.6.2

代碼:

#!/bin/bash
# ==============================================================================
# chmod u+x /opt/nginx/cut_nginx_log.sh
# crontab -e
# 0 0 * * * /opt/nginx/cut_nginx_log.sh > /opt/nginx/logs/cut_nginx_log.log 2>1
# ==============================================================================

LOGS_PATH="/opt/nginx/logs"
ARCHIVE_YEAR=$(date -d "yesterday" "+%Y")
ARCHIVE_MONTH=$(date -d "yesterday" "+%m")
ARCHIVE_DATE=$(date -d "yesterday" "+%Y%m%d_%H%M%S")
if [ -r /opt/nginx/nginx.pid ]; then
  mkdir -p "${LOGS_PATH}/${ARCHIVE_YEAR}/${ARCHIVE_MONTH}"
  mv "${LOGS_PATH}/access.log" "${LOGS_PATH}/${ARCHIVE_YEAR}/${ARCHIVE_MONTH}/access_${ARCHIVE_DATE}.log"
  kill -USR1 $(cat "/opt/nginx/nginx.pid")
  sleep 1
  gzip "${LOGS_PATH}/${ARCHIVE_YEAR}/${ARCHIVE_MONTH}/access_${ARCHIVE_DATE}.log"
else
  echo "Nginx might be down"
fi

# ==============================================================================
# Clean up log files older than 100 days
# ==============================================================================

# Change HOUSEKEEPING=1 to enable clean up
HOUSEKEEPING=0
KEEP_DAYS=100
if [ $HOUSEKEEPING == 1 ]; then
  if [ -d "${LOGS_PATH}" ]; then
    find "${LOGS_PATH}" -type f -name "access_*.log.gz" -mtime +${KEEP_DAYS} -exec rm -f {} \;
  fi
fi

參考:
http://wiki.nginx.org/LogRotation

您可能感興趣的文章:
  • 詳解Nginx日志配置及日志切割
  • nginx日志切割shell腳本
  • Crontab+Shell做Nginx日志切割腳本實例代碼

標(biāo)簽:巴彥淖爾 赤峰 許昌 馬鞍山 淘寶邀評 婁底 邵陽 金昌

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《nginx日志切割腳本分享》,本文關(guān)鍵詞  nginx,日志,切割,腳本,分享,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《nginx日志切割腳本分享》相關(guān)的同類信息!
  • 本頁收集關(guān)于nginx日志切割腳本分享的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章