试验环境:
# 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
参考:
阅读(2294) | 评论(0) | 转发(0) |