logrotate 无法正常工作

logrotate 无法正常工作

阿帕奇:2.4.29

我的要求是 error_log 需要轮换,并且 error_log 需要有最新的日志,error_log.1 应该有比 error_log 更旧的日志,error_log.2 应该有比 error_log.1 更旧的日志,依此类推。但问题是第一次轮换后 error_log 变空,而 error_log.1 拥有所有日志。之后只有 error_log.1 不断被日志填满,没有创建其他日志文件,error_log 也保持为空。以下是日志文件所在的位置:

[root@xxx]# ls -l
total 56
-rw-r--r-- 1 root root 37720 Jun 23 06:25 error_log
-rw-r--r-- 1 root root  4454 Jun 23 06:25 httpd.log

这是我的配置文件的位置:

[root@xxx]# ls
test_apache_logrotate.conf  logrotate.rsyslog  
[root@xxx]# ```

And here are the contents of the conf file:
```/tmp/tools/error_log {
    su root root
    rotate 7
    size 300
    nocompress
    missingok
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
            /usr/bin/pkill -HUP rsyslogd 2> /dev/null || true
    endscript
} ```


And then I run the logrotate on the conf file:
```[root@xxx]# /usr/sbin/logrotate -fv /etc/logrotate.d/test_apache_logrotate.conf
reading config file /etc/logrotate.d/test_apache_logrotate.conf
Reading state from file: /var/lib/logrotate.status
Allocating hash table for state file, size 64 entries
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state

Handling 1 logs

rotating pattern: /tmp/tools/error_log  forced from command line (7 rotations)
empty log files are not rotated, old logs are removed
considering log /tmp/tools/error_log
  Now: 2023-06-23 06:35
  Last rotated at 2023-06-23 06:19
  log needs rotating
rotating log /tmp/tools/error_log, log->rotateCount is 7
dateext suffix '-20230623'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
moving /tmp/tools/error_log.7 to /tmp/tools/error_log.8 (rotatecount 7, logstart 1, i 7),
old log /tmp/tools/error_log.7 does not exist
moving /tmp/tools/error_log.6 to /tmp/tools/error_log.7 (rotatecount 7, logstart 1, i 6),
old log /tmp/tools/error_log.6 does not exist
moving /tmp/tools/error_log.5 to /tmp/tools/error_log.6 (rotatecount 7, logstart 1, i 5),
old log /tmp/tools/error_log.5 does not exist
moving /tmp/tools/error_log.4 to /tmp/tools/error_log.5 (rotatecount 7, logstart 1, i 4),
old log /tmp/tools/error_log.4 does not exist
moving /tmp/tools/error_log.3 to /tmp/tools/error_log.4 (rotatecount 7, logstart 1, i 3),
old log /tmp/tools/error_log.3 does not exist
moving /tmp/tools/error_log.2 to /tmp/tools/error_log.3 (rotatecount 7, logstart 1, i 2),
old log /tmp/tools/error_log.2 does not exist
moving /tmp/tools/error_log.1 to /tmp/tools/error_log.2 (rotatecount 7, logstart 1, i 1),
old log /tmp/tools/error_log.1 does not exist
moving /tmp/tools/error_log.0 to /tmp/tools/error_log.1 (rotatecount 7, logstart 1, i 0),
old log /tmp/tools/error_log.0 does not exist
log /tmp/tools/error_log.8 doesn't exist -- won't try to dispose of it
moving /tmp/tools/error_log to /tmp/tools/error_log.1
creating new /tmp/tools/error_log mode = 0640 uid = 0 gid = 4
running postrotate script

旋转后:

[root@xxx]# ls -l
total 56
-rw-r----- 1 root adm      0 Jun 23 06:35 error_log
-rw-r--r-- 1 root root 37720 Jun 23 06:25 error_log.1
-rw-r--r-- 1 root root  4454 Jun 23 06:25 httpd.log

[root@xxx]#```

Thus it can be see that error_log is not having any contents.
Also after this I ran curl command from a client machine, after which the log contents changed:
```[root@xxx]# cd /tmp/tools/
[root@xxx]# ls -l
total 412
-rw-r----- 1 root adm       0 Jun 23 06:35 error_log
-rw-r--r-- 1 root root 398696 Jun 23 06:46 error_log.1
-rw-r--r-- 1 root root  11214 Jun 23 06:46 httpd.log ```
From the above it can seen that error_log.1 contents have changed.

I again give the logrotate on the conf file now:
```[root@xxx]# /usr/sbin/logrotate -fv /etc/logrotate.d/test_apache_logrotate.conf
reading config file /etc/logrotate.d/test_apache_logrotate.conf
Reading state from file: /var/lib/logrotate.status
Allocating hash table for state file, size 64 entries
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state

Handling 1 logs

rotating pattern: /tmp/tools/error_log  forced from command line (7 rotations)
empty log files are not rotated, old logs are removed
considering log /tmp/tools/error_log
  Now: 2023-06-23 06:50
  Last rotated at 2023-06-23 06:35
  log does not need rotating (log is empty)
not running postrotate script, since no logs were rotated ```
Now checking if the logs are rotated:
'''[root@xxx]# cd /tmp/tools/
[root@xxx]# ls -l
total 540
-rw-r----- 1 root adm       0 Jun 23 06:35 error_log
-rw-r--r-- 1 root root 524839 Jun 23 06:49 error_log.1
-rw-r--r-- 1 root root  13710 Jun 23 06:46 httpd.log

从上面我们可以看出日志没有被轮换。

/var/lib/logrotate.status:“/tmp/webtools/error_log”2023-6-23-6:35:34

相关内容