您好,登錄后才能下訂單哦!
這篇文章主要介紹“nginx日志切割的腳本分享”,在日常操作中,相信很多人在nginx日志切割的腳本分享問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”nginx日志切割的腳本分享”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
實現方法一
#!/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
實現方法二
試驗環境:
# 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
到此,關于“nginx日志切割的腳本分享”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。