Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App writes to ".log.1" file instead of ".log" file after running logrotate

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?

like image 631
Tung Nguyen Avatar asked Mar 05 '14 09:03

Tung Nguyen


People also ask

Where is the log rotation configuration for all log files located?

The main logrotate configuration file is located at /etc/logrotate. conf . The file contains the default parameters that logrotate uses when it rotates logs.

How do you rotate logs with logrotate?

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.

What is Missingok in logrotate?

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).


1 Answers

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.

like image 121
Tung Nguyen Avatar answered Oct 26 '22 21:10

Tung Nguyen