2

For some reason I'm not being able to use logotate to compress files periodically.
So I decided to write my own script, it's not the most hard thing but one small detail is pinning me down.
When using zip to compress the file , logrotate keeps the original files and empty them. I can use gzip then echo to achieve the same result but assuming my application is writing a lot of data into logs , there will be certainly some lost logs in timestamps after running gzip and before echo

gzip -k file.log
echo "" > file.log

Any idea how can I make it ? Am I missing an option of gzip that empties the original file ? Thanks :)

Edit

Solved by sending signal to the process (which is a node script) and enforce it to reopen the log file as suggested by AlexP.

2

logrotate works by (1) renaming the log file as if using mv, (2) creating a new file with the old name, (3) signalling somehow to the process(es) writing to the log file to close and reopen it (this switches them to the new file), and finally (4) compressing the renamed file.

The tricky step is of course (3), because how to tell a process that it should close and reopen its log(s) depends on the specific process. For example, if you look in /etc/logrotate.d/apache2, it uses the reload command to make Apache HTTP Server close and reopen the logs, while /etc/logrotate.d/samba shows that for Samba, it sends the signal SIGHUP.

1
  • Indeed , I need to force the process to write to the new file without restarting the service , actually logrotate is doing it well (without defining any postrotate directive in conf ) – storm Dec 5 '16 at 21:57
1

Here's how you can do it, open a terminal and type :

cat file.log | gzip -9 > tmp_file.gz && echo > file.log
1
  • Just tested .. will not work if the original file is sized enough to take time to be archived .. some log will be lost – storm Dec 4 '18 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.