PM2 Logrotate keeps generating new logs

14,776

Solution 1

I had the same problem as you. Change max_size from 100MB to 100M. The documentation has the default for max_size as 10MB but options it gives are 10G, 10M, 10K. Apparently it doesn't know what to do with MB vs M.

Solution 2

According to the documentation, here's how you want to schedule log-rotate:

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

https://github.com/pm2-hive/pm2-logrotate

If you set rotateInterval '* * 23 * * *', log-rotate will rotate the log every second of every minute of the 23rd hour.

I don't think you can rotate every 23 hours other than to change the setting daily, but to rotate on the 23rd hour of each day, you'd set it like this: rotateInterval '0 0 23 * * *'

Note also that since the 'second' setting is optional, so you could express this more simply as: rotateInterval '0 23 * * *'

Edit:

This will explicitly set all pm2-logrotate options to their default values:

pm2 set pm2-logrotate:retain 7
pm2 set pm2-logrotate:compress false 
pm2 set pm2-logrotate:dateFormat YYYY-MM-DD_HH-mm-ss 
pm2 set pm2-logrotate:max_size 10M 
pm2 set pm2-logrotate:retain 7 
pm2 set pm2-logrotate:rotateInterval '0 0 * * * '
pm2 set pm2-logrotate:rotateModule true 
pm2 set pm2-logrotate:workerInterval 30      
Share:
14,776
Almir
Author by

Almir

Updated on June 23, 2022

Comments

  • Almir
    Almir almost 2 years

    I have following settings set for PM2 Logrotate:

    rotateInterval= * * 23 * * *
    max_size= 100MB   
    

    I want to rotate the logs every 23 hours and/or keep max 100MB log files. But for some odd reason, the PM2 Logrotate keeps rotating / creating new log files way too often so I loose the ability to see the history of the logs, i. e pm2 logs --lines 300 only displays that a new log has been created. This is the output I see in the terminal:

    pm2-logrotate > "/root/.pm2/logs/scraper-init-out-1__2017-06-01_08-00-25.log" has been created
    
    pm2-logrotate > "/root/.pm2/logs/pm2-logrotate-out-2__2017-06-01_08-00-25.log" has been created  
    
    pm2-logrotate > "/root/.pm2/logs/pm2-logrotate-out-2__2017-06-01_08-00-55.log" has been created                                                       
    
    pm2-logrotate > "/root/.pm2/logs/pm2-logrotate-out-2__2017-06-01_08-01-25.log" has been created  
    

    What am I doing wrong here?