/etc/logrotate.d/目录存在各种服务:如squid、nginx、httpd等
 

  1. # less /etc/logrotate.d/squid    
  2. /web/squid/logs/access.log /web/squid/logs/cache.log /web/squid/logs/store.log {  
  3.     daily  
  4.     missingok  
  5.     nocompress  
  6.     noolddir  
  7.     sharedscripts  
  8.     postrotate  
  9.         DATE=`/bin/date --date=yesterday +%y%m%d`  
  10.         LOGDIR="/web/squid/logs" 
  11.         /usr/sbin/squid -k rotate 2>/dev/null || true 
  12.         sleep 10  
  13.         for LOGFILE in ${LOGDIR}/*.log; do  
  14.           [ -f ${LOGFILE}.1 ] && mv ${LOGFILE}.1 ${LOGFILE}-${DATE}  
  15.           [ -f ${LOGFILE}-${DATE} ] && /bin/gzip ${LOGFILE}-${DATE}  
  16.         done  
  17.         /usr/bin/find ${LOGDIR}/ -type f -name "*.log-*.gz" -mtime +180 -exec rm -f {} \;  
  18.     endscript  



该脚本执行logrotate命令:
 

  1. # cat /etc/cron.daily/logrotate  
  2. #!/bin/sh  
  3.  
  4. /usr/sbin/logrotate /etc/logrotate.conf  
  5. EXITVALUE=$?  
  6. if [ $EXITVALUE != 0 ]; then 
  7.     /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" 
  8. fi  
  9. exit 0 



conf配置文件,注意include选项

# cat /etc/logrotate.conf
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

查看/etc/crontab文件,这是crontab的默认参数
 

  1. # cat /etc/crontab  
  2. SHELL=/bin/bash  
  3. PATH=/sbin:/bin:/usr/sbin:/usr/bin  
  4. MAILTO=root  
  5. HOME=/  
  6.  
  7. # run-parts  
  8. 01 * * * * root run-parts /etc/cron.hourly  
  9. 02 4 * * * root run-parts /etc/cron.daily  
  10. 22 4 * * 0 root run-parts /etc/cron.weekly  
  11. 42 4 1 * * root run-parts /etc/cron.monthly 



run-parts /etc/cron.daily  执行命令执行了改目录下的所有脚本,如下:
# ls /etc/cron.daily/
0logwatch  cups  logrotate  makewhatis.cron  mlocate.cron  prelink  rpm  tmpwatch


这个流程是,cron 负责调度,crond 读 /etc/crontab 获得配置, /etc/crontab 包含 /etc/cron.daily 目录下的所有配置,而 logrotate 则是其中的一个任务;logrotate 每天 4 点 2 分开始跑,logrotate 又读自己的配置 /etc/logrotate.conf,logrotate.conf 包含 /etc/logrotate.d 下的配置,squid 是其中的一个。

为了避免重复操作,logrotate 会记一个状态在 /var/lib/logrotate.status 文件中,故/usr/sbin/logrotate /etc/logrotate.conf   命令一直无效,-f参数强制执行会覆盖已有的数据