Logrotate
logrotate ‐ rotates, compresses, and mails system logs
Categories
- Cron:Examples:LogFileRotate - 날짜별로 로그파일 자동 분할 crontab 예제.
SYNOPSIS
logrotate [--force] [--debug] [--state file] [--skip-state-lock] [--verbose] [--log file] [--mail command] config_file [config_file2 ...]
DESCRIPTION
logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.
Normally, logrotate is run as a daily cron job. It will not modify a log more than once in one day unless the criterion for that log is based on the log's size and logrotate is being run more than once each day, or unless the -f or --force option is used.
Any number of config files may be given on the command line. Later config files may override the options given in earlier files, so the order in which the logrotate config files are listed is important. Normally, a single config file which includes any other config files which are needed should be used. See below for more information on how to use the include directive to accomplish this. If a directory is given on the command line, every file in that directory is used as a config file.
If no command line arguments are given, logrotate will print version and copyright information, along with a short usage summary. If any errors occur while rotating logs, logrotate will exit with non-zero status, although the state file will be updated.
About
커널이나 각 데몬의 메시지를 계속 기록하게 되면 로그 파일 크기가 커져 파일 시스템의 파일 용량 상한에 도달하게 되고 메시지를 기록할 수 없게 된다. 이를 방지하기 위한 것이 logrotate이다.
logrotate는 cron 경유로 구동되며 지정된 로그 파일을 이동, 압축하고 새로운 로그 파일을 생성한다. 또한, 필요하면 데몬을 재구동하기도 한다.
처리 순서
- cron이
/etc/cron.daily/logrotate
를 읽어들임 - logrotate가
/etc/logrotate.conf
를 설정파일로써 실행하여logrotate.conf
에 지정된 로그 파일을 처리함 -
/etc/logrotate.d/
아래의 파일을 읽어들여 지정된 로그 파일을 처리함
주요 logrotate 지시어 리스트
- create
- missingok
- rotate
- daily, weekly, monthly, yearly
- compress
- delaycompress
- compresscmd
- postrotate/endscript
- notifempty
- sharedscripts
- size/minsize
- nocreate
See also
Troubleshooting
Free-space driven log rotation
디스크의 남은 공간을 확인하고 제거하는 logrotate:
/mnt/user/logs/*.log /mnt/user/logs/*/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
su root www-data
create 760 root www-data
firstaction
for file in `find -type f -size +1024M`; do
percent=`df -h | grep /mnt/user | awk '{print $5}' | sed 's/%//'`
if [ $percent -gt 50 ]; then
echo "Removed $file" >> /mnt/user/logs/logrotate.log
rm $file
fi
done;
endscript
}