Rotate and Compress pm2 logs Elegantly

36 sec read

pm2 logs can grow big overtime. As big as 50GB or more in a month time based on your application usage. Eventually you may run out of disk space. Here are the simple steps to manage it.
1: Install logrotate
sudo apt install logrotate
2: Install pm2-log rotate, which comes with loads of useful features. This should take care of auto rotating, compressing and managing logs.
sudo pm2 install pm2-logrotate
If you get an error saying pm2: command not found, then install pm2 using the command sudo npm i -g pm2
Additional step: If you want to make sure the pm2-logrotate makes use of native logrorate, then run the command
sudo pm2 logrotate -u your-server-username .
To make sure the native logrotate is working fine, run the command:
logrotate -f /etc/logrotate.d/pm2-your-server-username
While this will show empty output and return to command prompt, you will be able to see certain log files compressed (.gz files) at the usual logs folder location of pm2, that is at ~/.pm2/logs

Related posts:

3 Replies to “Rotate and Compress pm2 logs Elegantly”

  1. I tried to install **pm2-logrotate** got
    deploy@wgn-staging:~$ sudo pm2 install pm2-logrotate
    sudo: pm2: command not found
    then ran the below command
    sudo npm i -g pm2
    After that **sudo pm2 install pm2-logrotate** ran successfully. Can you please add these steps also in your blog.

Leave a Reply

Your email address will not be published. Required fields are marked *