검색결과 리스트
logrotate에 해당되는 글 2건
글
리눅스 logrotate 에서 로그파일 강제로 rotate 시키기
리눅스 계열 서버에서 각종 로그를 로테이트시키는 기능은 logrotate 가 담당한다.
그런데, 이 logrotate 는 cron 데몬에 의해서 실행이 된다. ^^
1. logrotate 가 실행되는 과정을 살펴 보자.
/etc/crontab 파일에 보면 아래와 같은 구문으로 매일 실행되는 것들의 폴더 위치가 있다.
02 4 * * * root run-parts /etc/cron.daily
위 폴더를 찾아가서 보면, logrotate 가 있다.
그러므로, 로그로테이트는 4시02분에 하루에 한번씩 실행된다.
logrotate 파일을 열어 보면.. 아래와 같은 구문이 있다.
/usr/sbin/logrotate /etc/logrotate.conf
실제 실행파일의 위치와 설정파일의 위치를 알 수 있다.
이제 /etc/logrotate.conf 파일을 열어 보자..
주요 설정들이 있는데, 역시나 아래와 같은 설정파일을 따로 빼 놓은 인클루드 구문이 있다.
include /etc/logrotate.d
/etc/logrotate.d 폴더에 들어가 보면.. 이제 개별로 로테이트 시킬 로그에 대한 설정들이 들어 있다. ^^
오늘 살펴볼 아파치 서버의 로그를 로테이트 시키는 놈은 apache 나 httpd 와 같은 이름으로 되어 있다.
물론 다른 이름으로 되어 있을 수도 있으나, 누가 보더라도 직관적인 이름이 좋다.
파일을 열어 보면.. 아래는 필자의 설정파일 이다.
/var/log/httpd/*-access_log /var/log/httpd/*-error_log {
size 500k
rotate 19
missingok
compress
postrotate
/usr/bin/killall -HUP httpd
endscript
}
위 세팅에 대한 설명은..
virtual host 세팅으로 여러 도메인의 로그가 따로 저장되어 있으며,
로그파일 사이즈가 500k 가 넘으면 로테이트 시키고 지난 19 건 이전 것은 삭제한다. 압축저장한다.
또다른 설정을 만들어 보면..
/var/log/httpd/*-access_log /var/log/httpd/*-error_log {
missingok
rotate 5
weekly
notifempty
sharedscripts
postrotate
/usr/local/apache/bini/apachectl restart
endscript
}
위 세팅의 조건은 매주 실행되고 빈파일은 로테이트 하지않으며 5건 이전 것은 삭제한다.
2. 강제로 로테이트를 시켜 보자..
위 설정대로 일주일에 한번만 실행하라고 세팅하면, 오늘 실행후 일주일을 기다려야 한다.
하지만, 잘 되는지 확인해 보고자 할 때가 있다.
이때는, logrotate 가 실행된 날짜가 저장되어 있는 파일을 찾아 날짜를 조작하면 된다.
해당 파일은 /var/lib/logrotate.status 이다.
열어 보면 아래와 같은 구문이 보인다.
"/var/log/httpd/www.domain.com-access_log" 2014-10-1
여기서 맨 뒤에 있는 날짜가 마지막 실행된 날짜 이므로 이 날짜를 일주일 전으로 수정하면 된다.
그리고, logrotate 를 실행한다.
# /etc/cron.daily/logrotate
로그 파일을 살펴보면, 로그가 로테이트 되었음을 볼 수 있다.
* 참고 및 이미지 출처 : http://www.thegeekstuff.com/2010/07/logrotate-examples/#more-4826
'서버관리' 카테고리의 다른 글
유닉스 리눅스 du 명령어 에서 sort 로 정렬하기 (0) | 2014.10.07 |
---|---|
CentOS 5.x 에서 curlftpfs 로 ftp filesystem mount 하기 (0) | 2014.10.03 |
HowTo: The Ultimate Logrotate Command Tutorial with 10 Examples (0) | 2014.10.01 |
apache 아파치 웹서버에서 디렉토리 목록 보이지 않게 설정하기 (0) | 2014.09.30 |
설정
트랙백
댓글
글
HowTo: The Ultimate Logrotate Command Tutorial with 10 Examples
리눅스 로그로테이트 설정 파일 예제
* 출처 : http://www.thegeekstuff.com/2010/07/logrotate-examples/#more-4826
1. Logrotate Configuration files
Following are the key files that you should be aware of for logrotate to work properly.
/usr/sbin/logrotate – The logrotate command itself.
/etc/cron.daily/logrotate – This shell script executes the logrotate command everyday.
$ cat /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
/etc/logrotate.conf – Log rotation configuration for all the log files are specified in this file.
$ cat /etc/logrotate.conf
weekly
rotate 4
create
include /etc/logrotate.d
/var/log/wtmp {
monthly
minsize 1M
create 0664 root utmp
rotate 1
}
/etc/logrotate.d – When individual packages are installed on the system, they drop the log rotation configuration information in this directory. For example, yum log rotate configuration information is shown below.
$ cat /etc/logrotate.d/yum
/var/log/yum.log {
missingok
notifempty
size 30k
yearly
create 0600 root root
}
2. Logrotate size option: Rotate the log file when file size reaches a specific limit
If you want to rotate a log file (for example, /tmp/output.log) for every 1KB, create the logrotate.conf as shown below.
$ cat logrotate.conf
/tmp/output.log {
size 1k
create 700 bala bala
rotate 4
}
This logrotate configuration has following three options:
size 1k – logrotate runs only if the filesize is equal to (or greater than) this size.
create – rotate the original file and create the new file with specified permission, user and group.
rotate – limits the number of log file rotation. So, this would keep only the recent 4 rotated log files.
Before the logrotation, following is the size of the output.log:
$ ls -l /tmp/output.log
-rw-r--r-- 1 bala bala 25868 2010-06-09 21:19 /tmp/output.log
Now, run the logrotate command as shown below. Option -s specifies the filename to write the logrotate status.
$ logrotate -s /var/log/logstatus logrotate.conf
Note : whenever you need of log rotation for some files, prepare the logrotate configuration and run the logroate command manually.
After the logrotation, following is the size of the output.log:
$ ls -l /tmp/output*
-rw-r--r-- 1 bala bala 25868 2010-06-09 21:20 output.log.1
-rwx------ 1 bala bala 0 2010-06-09 21:20 output.log
Eventually this will keep following setup of rotated log files.
output.log.4.
output.log.3
output.log.2
output.log.1
output.log
Please remember that after the log rotation, the log file corresponds to the service would still point to rotated file (output.log.1) and keeps on writing in it. You can use the above method, if you want to rotate the apache access_log or error_log every 5 MB.
Ideally, you should modify the /etc/logrotate.conf to specify the logrotate information for a specific log file.
Also, if you are having huge log files, you can use: 10 Awesome Examples for Viewing Huge Log Files in Unix
3. Logrotate copytruncate option: Continue to write the log information in the newly created file after rotating the old log file.
$ cat logrotate.conf
/tmp/output.log {
size 1k
copytruncate
rotate 4
}
copytruncate instruct logrotate to creates the copy of the original file (i.e rotate the original log file) and truncates the original file to zero byte size. This helps the respective service that belongs to that log file can write to the proper file.
While manipulating log files, you might find the sed substitute, sed delete tips helpful.
4. Logrotate compress option: Compress the rotated log files
If you use the compress option as shown below, the rotated files will be compressed with gzip utility.
$ cat logrotate.conf
/tmp/output.log {
size 1k
copytruncate
create 700 bala bala
rotate 4
compress
}
Output of compressed log file:
$ ls /tmp/output*
output.log.1.gz output.log
5. Logrotate dateext option: Rotate the old log file with date in the log filename
$ cat logrotate.conf
/tmp/output.log {
size 1k
copytruncate
create 700 bala bala
dateext
rotate 4
compress
}
After the above configuration, you’ll notice the date in the rotated log file as shown below.
$ ls -lrt /tmp/output*
-rw-r--r-- 1 bala bala 8980 2010-06-09 22:10 output.log-20100609.gz
-rwxrwxrwx 1 bala bala 0 2010-06-09 22:11 output.log
This would work only once in a day. Because when it tries to rotate next time on the same day, earlier rotated file will be having the same filename. So, the logrotate wont be successful after the first run on the same day.
Typically you might use tail -f to view the output of the log file in realtime. You can even combine multiple tail -f output and display it on single terminal.
6. Logrotate monthly, daily, weekly option: Rotate the log file weekly/daily/monthly
For doing the rotation monthly once,
$ cat logrotate.conf
/tmp/output.log {
monthly
copytruncate
rotate 4
compress
}
Add the weekly keyword as shown below for weekly log rotation.
$ cat logrotate.conf
/tmp/output.log {
weekly
copytruncate
rotate 4
compress
}
Add the daily keyword as shown below for every day log rotation. You can also rotate logs hourly.
$ cat logrotate.conf
/tmp/output.log {
daily
copytruncate
rotate 4
compress
}
7. Logrotate postrotate endscript option: Run custom shell scripts immediately after log rotation
Logrotate allows you to run your own custom shell scripts after it completes the log file rotation. The following configuration indicates that it will execute myscript.sh after the logrotation.
$ cat logrotate.conf
/tmp/output.log {
size 1k
copytruncate
rotate 4
compress
postrotate
/home/bala/myscript.sh
endscript
}
8. Logrotate maxage option: Remove older rotated log files
Logrotate automatically removes the rotated files after a specific number of days. The following example indicates that the rotated log files would be removed after 100 days.
$ cat logrotate.conf
/tmp/output.log {
size 1k
copytruncate
rotate 4
compress
maxage 100
}
9. Logrotate missingok option: Dont return error if the log file is missing
You can ignore the error message when the actual file is not available by using this option as shown below.
$ cat logrotate.conf
/tmp/output.log {
size 1k
copytruncate
rotate 4
compress
missingok
}
10. Logrotate compresscmd and compressext option: Sspecify compression command for the log file rotation
$ cat logrotate.conf
/tmp/output.log {
size 1k
copytruncate
create
compress
compresscmd /bin/bzip2
compressext .bz2
rotate 4
}
Following compression options are specified above:
compress – Indicates that compression should be done.
compresscmd – Specify what type of compression command should be used. For example: /bin/bzip2
compressext – Specify the extension on the rotated log file. Without this option, the rotated file would have the default extension as .gz. So, if you use bzip2 compressioncmd, specify the extension as .bz2 as shown in the above example.
'서버관리' 카테고리의 다른 글
유닉스 리눅스 du 명령어 에서 sort 로 정렬하기 (0) | 2014.10.07 |
---|---|
CentOS 5.x 에서 curlftpfs 로 ftp filesystem mount 하기 (0) | 2014.10.03 |
리눅스 logrotate 에서 로그파일 강제로 rotate 시키기 (0) | 2014.10.03 |
apache 아파치 웹서버에서 디렉토리 목록 보이지 않게 설정하기 (0) | 2014.09.30 |
RECENT COMMENT