Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PM2 Logrotate keeps generating new logs

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?

like image 608
Almir Avatar asked Jun 01 '17 08:06

Almir


People also ask

What is pm2 log rotate?

The module pm2-logrotate automatically rotate and keep all the logs file using a limited space on disk.

Where are pm2 logs stored?

Access the logs By default, all logs are saved into $HOME/. pm2/logs .

Does logrotate create new file?

In this mode, logrotate will rename a log file—for example, an Apache error log file located in the /var/log/apache2 directory—to error. log. 1, and then create a new error. log file in the same directory.

How do you check pm2 logs?

Type pm2 log in a shell and you get an overview of the last 15 log lines and the last 15 error lines in realtime. At the top of these log lines, the location of your logfile is shown. You can also type pm2 log --lines 1000 to show more lines, in this case 1000. Save this answer.


2 Answers

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.

like image 78
erics2783 Avatar answered Sep 30 '22 13:09

erics2783


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      
like image 41
josebrwn Avatar answered Sep 30 '22 12:09

josebrwn