2015年1月7日 星期三

設定 nginx logrotate‏ 執行的時段

先增加 nginx logrotate 的設定,在 /etc/logrotate.d/ 底下新增一個 nginx 檔案,內容為
/usr/local/nginx/logs/*.log {
daily
rotate 30
# If the log file is missing, go on to the next one without issuing an error message.
missingok
# Do not rotate the log if it is empty (this overrides the ifempty option).
notifempty
sharedscripts
postrotate
    if [ -f /usr/local/nginx/logs/nginx.pid ]; then
        kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
    fi
endscript
}
sharedscripts:所有 log 都處理完,才執行一次 script
(Normally, prerotate and postrotate scripts are run for each log which is rotated and the absolute path to the log file  is  passed as  first  argument  to the script. That means a single script may be run multiple times for log file entries which match multiple files (such as the /var/log/news/* example). If sharedscripts is specified, the scripts are only run once, no matter how many logs match  the wildcarded pattern, and whole pattern is passed to them.  However, if none of the logs in the pattern require rotating, the scripts will not be run at all. If the scripts exit with error, the remaining actions will not be executed for any logs.  This option overrides the nosharedscripts option and implies create option.)


強制執行一次 logrotate,測試是否正常
logrotate -vf /etc/logrotate.d/nginx


若要指定每天幾點 logrotate,可加入排程中,例如
# crontab -e
59 23 * * *  /usr/sbin/logrotate -f /etc/logrotate.d/nginx


在 CentOS 是使用 anacron 來設定 logrotate 執行的時段,可修改 /etc/anacrontab 裡面 START_HOURS_RANGE 的設定
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
#START_HOURS_RANGE=3-22
#改成早上 3~5 點的時候執行
START_HOURS_RANGE=3-5

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly


參考
使用logrotate管理nginx日志文件
Why does my CentOS logrotate run at random times?
Configuring anacron Jobs
OEL6 中的 Cron Daily 是何時執行呢?
CentOS 6.4 (64位元) 定期排程 以備份 Moodle 網站為例
nginx logrotate config
Daily logRotate for apache at specific time

沒有留言:

張貼留言