ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • nginx log 파일 정리 스크립트
    nginx 2021. 12. 9. 21:53

    문제

    오래된 nginx 로그 파일을 logrotate 를 활용해 정리할 수 있지만, shell script 로 직접 정리할 수 있도록 해 보자

    코드

    #!/bin/bash
    
    # File date format
    DATE=`/bin/date --date="1 days ago" +%Y%m%d`
    
    # Archive period
    DAYS=30
    NGINX_PID_FILE=/opt/nginx/logs/nginx.pid
    NGINX_LOG_DIR=/opt/nginx/logs
    NGINX_LOG_ARCHIVE_DIR=/opt/nginx/logs
    
    ### Delete Nginx log ###
    function rotate_nginx_log {
        mv $NGINX_LOG_DIR/access.log $NGINX_LOG_ARCHIVE_DIR/access.log.$DATE
        mv $NGINX_LOG_DIR/error.log $NGINX_LOG_ARCHIVE_DIR/error.log.$DATE
        kill -USR1 `cat $NGINX_PID_FILE`
        find $NGINX_LOG_ARCHIVE_DIR -mtime +$DAYS -name "access.log.*" -exec rm {} \;
        find $NGINX_LOG_ARCHIVE_DIR -mtime +$DAYS -name "error.log.*" -exec rm {} \;
    }
    
    ### Main ###
    rotate_nginx_log

    위의 스크립트를 crontab 에 등록해 매일 실행될 수 있도록 한다.

    'nginx' 카테고리의 다른 글

    nginx 설치  (0) 2021.12.27

    댓글

Designed by Tistory.