After force running logrotate
, my app keeps writing to my_app.log.1
(the old log that should be archived later) instead of my_app.log
.
This make my_app.log
an empty file, therefore logrotate
runs without any effect. And my_app.log.1
keep growing to gigabytes.
I am running Ubuntu 12.04. My app is a Node.js app using pm2. Following is my logrotate configuration:
"/var/log/my_app/*.log" {
daily
size 50M
rotate 10
missingok
compress
delaycompress
notifempty
}
I know I put notifempty
there, but why is my_app.log.1
written to in the first place?
The main logrotate configuration file is located at /etc/logrotate. conf . The file contains the default parameters that logrotate uses when it rotates logs.
Handling Active Log File. In the default behavior, the rotation process beings by logrotate renaming the active log file into a different name. Then, it creates a new log file with the same name. Finally, it invokes other logic such as compress and mail to complete the rotation process.
missingok : If the log file is missing, go on to the next one without issuing an error message. noolddir : Logs are rotated in the same directory the log normally resides in (this overrides the olddir option).
I finally figured out how to solve the problem.
This was because the log file was being written by pm2
. logrotate
changed its name to my_app.log.1
and created new my_app.log
file, but pm2
did not care about this and kept writing to my_app.log.1
.
I solved the problem by replacing the notifempty
option by copytruncate
and then restarted pm2
. After fixing, notifempty
can be added back but I do not really need it.
See the logrotate
reference for more information. Hope this will help other folks getting a similar problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With