Copytruncate在日志中不起作用

2024-05-16 16:31:52 发布

您现在位置:Python中文网/ 问答频道 /正文

截断原始文件在logrotate中不起作用。 我正在使用mosquito并尝试用logrotate生成日志文件。想每天分开日志文件。但在保存昨天的日志文件后,我无法截断(或清理)原始日志文件,所以消息已附加。结果如下。在

root@ip-172-31-35-251:/mnt/s3/logs/mosquitto# pwd

/mnt/s3/logs/mosquitto

root@ip-172-31-35-251:/mnt/s3/logs/mosquitto# ll

total 28996
drwxr-xr-x 1 root root        0 Nov  5 01:58 ./
d--------- 1 root root        0 Aug 18 04:36 ../
-rw-r--r-- 1 root root 29690237 Nov 10 04:23 mosquitto.log

root@ip-172-31-35-251:/mnt/s3/logs/mosquitto# cat /etc/logrotate.d/mosquitto
/mnt/s3/logs/mosquitto/mosquitto.log {
   notifempty
   compress
   copytruncate
   dateext
}

root@ip-172-31-35-251:/mnt/s3/logs/mosquitto# sudo logrotate -fv /etc/logrotate.d/mosquitto
reading config file /etc/logrotate.d/mosquitto
Handling 1 logs
rotating pattern: /mnt/s3/logs/mosquitto/mosquitto.log  forced from command line (no old logs will be kept)
empty log files are not rotated, old logs are removed
considering log /mnt/s3/logs/mosquitto/mosquitto.log
 log needs rotating
rotating log /mnt/s3/logs/mosquitto/mosquitto.log, log->rotateCount is 0
dateext suffix '-20151110'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
copying /mnt/s3/logs/mosquitto/mosquitto.log to /mnt/s3/logs/mosquitto/mosquitto.log-20151110
truncating /mnt/s3/logs/mosquitto/mosquitto.log
compressing log with: /bin/gzip

root@ip-172-31-35-251:/mnt/s3/logs/mosquitto# ll

total 30623
drwxr-xr-x 1 root root        0 Nov  5 01:58 ./
d--------- 1 root root        0 Aug 18 04:36 ../
-rw-r--r-- 1 root root 29700957 Nov 10 04:23 mosquitto.log
-rw------- 1 root root  1655769 Nov 10 04:23 mosquitto.log-20151110.gz

root@ip-172-31-35-251:/mnt/s3/logs/mosquitto# head mosquitto.log

1446719222: mosquitto version 1.4.2 (build date 2015-08-06 07:31:47+0000) starting
1446719222: Config loaded from /etc/mosquitto/mosquitto.conf.
1446719222: Opening ipv4 listen socket on port 8883.
1446719222: Opening ipv6 listen socket on port 8883.
1446719223: New connection from 127.0.0.1 on port 8883.
1446719223: New client connected from 127.0.0.1 as paho/9546DA4599D1DD306C (c1, k60, u'server').
1446719223: Sending CONNACK to paho/9546DA4599D1DD306C (0, 0)
1446719223: Received SUBSCRIBE from paho/9546DA4599D1DD306C
1446719223: $SYS/# (QoS 0)
1446719223: paho/9546DA4599D1DD306C 0 $SYS/#

root@ip-172-31-35-251:/mnt/s3/logs/mosquitto#

我想这个问题与linux或文件描述符的写锁有关。所以我附加了我现在使用的代码。在

^{pr2}$

Python脚本通过管道从mosquitto进程的stdout获取日志。 下面是mosquitto的运行代码。在

start-stop-daemon --start --quiet --oknodo --background --make-pidfile --pidfile ${PIDFILE} --startas /bin/bash -- -c "exec ${DAEMON} -c /etc/mosquitto/mosquitto.conf 2>&1 | /root/mosquittologger.py”

请帮忙。在


Tags: 文件fromiplogs3etcrootpaho