Chinaunix首页 | 论坛 | 博客
  • 博客访问: 346848
  • 博文数量: 38
  • 博客积分: 1289
  • 博客等级: 中尉
  • 技术积分: 533
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-10 22:30
个人简介

Good Good Study. Day Day Up.

文章分类

全部博文(38)

文章存档

2018年(1)

2016年(3)

2015年(12)

2014年(7)

2013年(10)

2012年(5)

分类: LINUX

2016-06-16 14:33:37

试验环境:

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

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

代码:

点击(此处)折叠或打开

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

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

  20. # ==============================================================================
  21. # Clean up log files older than 100 days
  22. # ==============================================================================

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

参考:

阅读(2294) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~