리눅스에서 서버를 운영하다보면, 서버에서 장애가 발생하거나 상태를 확인할 때 가장 먼저 보는것이 로그입니다. 그래서 대부분의 로그는 남기도록 설정하고, 백업을 통해 특정기간동안 보관하기 마련입니다. 하지만 이 로그들은 시간이 지나면서 하나의 파일로 관리하기에 감당할 수 없을만큼 엄청난 크기로 커지게 됩니다.
linux system의 다양한 기능들 중 로그를 관리해주는 logrotate라는 아주 유용한 기능이 있습니다.
logrotate는 기본적으로 crontab에서 시작합니다. crontab이 logrotate를 실행하고, logrotate는 logrotate.conf를 참조하여 기본적으로 /etc/logrotate.d 디렉토리에 있는 설정에 맞게 로그 롤링/삭제/백업을 진행합니다.
logrotate와 관련된 파일들은 CentOS7을 기준으로 아래 경로에 위치합니다.
- 데몬 프로그램:
/usr/sbin/logrotate
- logrotate conf:
/etc/logrotate.conf
- logrotate 프로세스 설정파일:
/etc/logrotate.d
/etc/logrotate.conf
를 참조하면 기본적으로 /etc/logrotate.d
디렉토리에 있는 설정파일들을 기반으로 수행됩니다. 그래서 /etc/logrotate.d
디렉토리에 아래와 같이 설정파일을 추가하면 자동으로 job이 수행됩니다.
1 2 3 4 5 6 7 8 9 10 11 | /server/apache2/logs/*log { ## target log 파일 daily rotate 5 notifempty missingok compress sharedscripts postrotate ## logrotate 마지막에 수행할 명령어를 아래에 작성하면 된다. /server/apache2/bin/apachectl graceful endscript } | cs |
위와같이/etc/logrotate.d
에 설정파일을 두기만 해도 매일 관련 작업이 수행되지만 그 전에 강제실행을 해야할 필요가 있다거나, 정상작동하는지 확인하기 위해 직접 logrotate를 실행할 수 있습니다.
logrotate 실행
1 | $ /usr/sbin/logrotate -f /etc/logrotate.d/{conf file} | cs |
Logrotate 디버그 모드
1 | $ /usr/sbin/logrotate -d /etc/logrotate.d/{conf file} | cs |
Logrotate 실행과정 화면 표시
1 | $ /usr/sbin/logrotate -v /etc/logrotate.d/{conf file} | cs |
NGINX 에서 SIGUSR1 인터럽트
nginx 의 경우 USR1 시그널을 받으면 로그 파일을 다시 open 하는 용도로 사용합니다
1 | $ kill -USR1 {nginx master PID} | cs |
Logrotate 설정파일의 경우 파일 권한이 0644 혹은 0444로 맞춰야합니다
1 2 3 4 5 | $ sudo /usr/sbin/logrotate -d /etc/logrotate.d/{conf file} error: Ignoring /etc/logrotate.d/{conf file} because of bad file mode - must be 0644 or 0444. Allocating hash table for state file, size 15360 B Handling 0 logs | cs |
'Development > Etc' 카테고리의 다른 글
Apache Kafka를 이용한 단일 노드 / 메시지 전송 (0) | 2019.03.26 |
---|---|
한국 지도 시각화하기 및 gps 표시 ( D3.js v5를 이용하여) (0) | 2019.02.13 |
Python Flask[플라스크] 시작하기 - 1 (0) | 2019.01.20 |
Python Django[장고] 시작하기 - 2 (0) | 2019.01.18 |
Python Django[장고] 시작하기 - 1 (Pycharm, Anaconda를 이용하여) (0) | 2019.01.18 |