Truncate original file doesn’t work in logrotate. I’m using mosquitto and trying to make log file with logrotate. Want to seperate log file by everyday. But after save log file of yesterday, I can’t truncate(or clean) original log file so message has been appended. The result like below.
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#
I suppose this problem concerned with write lock by linux or file descriptor. So i append code which i'm using now.
#!/usr/bin/python
import sys
import time
import logging
logger = logging.getLogger("logger")
formatter = logging.Formatter('[%(asctime)s] %(message)s')
fileHandler = logging.FileHandler('/mnt/s3/logs/mosquitto/mosquitto.log')
fileHandler.setFormatter(formatter)
logger.addHandler(fileHandler)
logger.setLevel(logging.DEBUG)
try:
buff = ''
while True:
buff += sys.stdin.read(1)
if buff.endswith('\n'):
logger.info(buff[:-1])
buff = ''
except KeyboardInterrupt:
sys.stdout.flush()
pass
Python script get log from stdout of mosquitto process via pipe. Running code of mosquitto is below.
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”
Please help.