ArmoArmo
ArmoArmo

Reputation: 105

logrotate create null bytes on log file after rotate python log file

I have the following logrotate.conf:

/path/to/my/file.log {
    weekly
    copytruncate
    rotate 5
    size 25
    nocompress
    sharedscripts
}

And I have the following bash script that execute my python:

python /app/myscript.py >> /path/to/my/file.log 2>&1&

After the logrotate run its create file.log with null bytes on begin, how can I solve it?

Upvotes: 1

Views: 735

Answers (2)

wizoot
wizoot

Reputation: 1

if your file is handled by a process The 'sed' comman will broke the fs entry for the origina file

and your process will write to a deleted file !!

ll -i /var/log/tomcat/17/jcxu11r2d2_log4j.log 33554504 -rwxrwxr-x 1 usrcot17 cot 15957988 May 11 13:56 /var/log/tomcat/17/jcxu11r2d2_log4j.log

grep -l -P '\x00' /var/log/tomcat/17/jcxu11r2d2_log4j.log /var/log/tomcat/17/jcxu11r2d2_log4j.log ll /proc/121203/fd/ | grep jcxu11r2d2_log4j.log lrwx------ 1 usrcot17 cot 64 May 11 02:09 210 -> /var/log/tomcat/17/jcxu11r2d2_log4j.log lrwx------ 1 usrcot17 cot 64 May 11 02:09 75 -> /var/log/tomcat/17/jcxu11r2d2_log4j.log sed -i -e 's/\o00//g' /var/log/tomcat/17/jcxu11r2d2_log4j.log ll -i /var/log/tomcat/17/jcxu11r2d2_log4j.log 33554503 -rwxrwxr-x 1 usrcot17 cot 832371 May 11 13:57 /var/log/tomcat/17/jcxu11r2d2_log4j.log grep -l -P '\x00' /var/log/tomcat/17/jcxu11r2d2_log4j.log ll /proc/121203/fd/ | grep jcxu11r2d2_log4j.log ll /proc/121203/fd/ | grep deleted lrwx------ 1 usrcot17 cot 64 May 11 02:09 210 -> /var/log/tomcat/17/sedzP6J1h (deleted) lrwx------ 1 usrcot17 cot 64 May 11 02:09 75 -> /var/log/tomcat/17/sedzP6J1h (deleted)

the null chars are due to a buffered write from your process https://access.redhat.com/solutions/135193

The only "good" solution i found at the moment is to let log rotation at the applicative process initiative

Upvotes: 0

Pranjal Doshi
Pranjal Doshi

Reputation: 1260

You can use postrotate to remove null value from your log.

/path/to/my/file.log {
    weekly
    copytruncate
    rotate 5
    size 25
    nocompress
    sharedscripts
    postrotate
       sed -i -e 's/\o00//g' "$1"
    endscript

}

Upvotes: 1

Related Questions