IT Knowledge/OS

Linux Logrotate 설정하기

Seok. 2025. 4. 28. 22:46
반응형

Logrotate 로그 파일을 일정시점, 파일 크기등 지정된 조건에 따라서 백업해주는 명령

일정시점 : daily, weekly..

파일크기 : 10MB .

 

설정파일 : /etc/logrotate.conf

  - Include /etc/logrotate.d : /etc/logrotate.d 디렉토리 하위의 모든 설정을 포함한다.

 

[예제]

/etc/logrotate.d/ansible

 

예제

/var/log/nginx/*.log {
  daily
  missingok
  rotate 52
  compress
  delaycompress
  notifempty
  create 640 nginx adm
  sharedscripts
  postrotate
    if [ -f /var/run/nginx.pid ]; then
        kill -USR1 `cat /var/run/nginx.pid`
    fi
  endscript
}

 

[옵션]

Daily, weekly, monthly, yearly : rotate 되는 주기를 설정한다.

Rotate 3 : rotate 파일을 보관할 개수를 지정한다.

Missingok : 파일이 없어도 오류를 발생시키지 않는다.

Notifempty : 파일이 비어있으면 rotate 하지 않는다.

Compress : rotate 로그파일을 압축한다.(default 지정)

Delaycompress : 로그 파일의 압축을 다음 순환 주기로 연기. (compress 함께 사용 필요)

 

Create 640 nginx nginx : 새로 로그 파일 생성시 640 모드로 생성하고 소유자,그룹은 nginx:nginx

 

postrotate: rotate 작업을 마친후에 실행할 스크립트를 적어줍니다. nginx 경우 SIGUSR1 받으면 로그 파일을 새로 읽으므로 새로 만들어진 로그 파일에 로그를 기록합니다.

( 부분이 제대로 설정되어 있지 않으면 nginx 이미 오픈한 로그 파일에 계속 기록합니다. 이로 인해 예전 로그 파일에 내용이 쌓이므로  주의해야 합니다.)

반응형