DEV Community


Posted on

How to save disk space when using PM2

TL;DR: Run pm2 install pm2-logrotate

PM2 is a process manager for Node.js, usually used in production

Yesterday I've ran into a problem on one of the production machines that I manage. Suddenly, the database stopped working and soon I found out that I was almost out of disk space.

Running df-h on a Linux machine

At first sight, I've checked my database records: all ok, as big as I was expecting.

Then, I had to start du -hing my largest directories to check what was taking so much space from my disk. I found the guilty one to be the ~/.pm2/logs folder, and instantly realized that my app's logs where just stacking up there. I solved temporarily with pm2 flush, but had to look for a way for that not to happen again.

And, then, I've found this guy:

It's as easy as running pm2 install pm2-logrotate on your machine. Done! PM2 will start to limit its logs size, by default to 10 megabytes, but it's easily configurable with something like pm2 set pm2-logrotate:max_size 1K. You can read more about the option here.

Top comments (4)

isaactru profile image

Let a server log errors overnight. Came back to a 35gb file. Thank you for this quick solution to fix pm2 log sizes!

fatihaziz profile image
Fatih Aziz

definitely me right now..

ducdev profile image
Duc Le

Crazy, same situation.

aldocosta profile image
Aldo Costa

Hey guys, do you know if this solutions works for a windows enviroments?