PM2 Log Rotation and Restart Strategies

Introduction to PM2

PM2 is a powerful process manager for Node.js applications, ensuring smooth and efficient operation.

Why Learn These PM2 Techniques?

  • Log Rotate Configuration:

    Efficient log management prevents disk space issues and simplifies troubleshooting.

  • Version Update

    Keeping PM2 updated ensures all features and restart strategies work correctly.

  • Restart Strategies:

    Automate restarts to maintain uptime and reliability.

PM2 Log Rotate Configuration Setup

The below table provide the steps and configurations needed to set up and manage log rotation using pm2-logrotate on a production server.

S. No

Task

Command / Path

1

■ Installation: 

→ To install pm2-logrotate

Installation
2

■ Configuration Verification

→ To verify the current configuration

Configuration Verification
3

■ Setting File Name Format

→ To set the format of the log file names

Setting File Name Format
4

■ Setting Rotation Interval 

→ To set the time interval for log rotation.

→ Use this command to rotate 
Every day at midnight

 


 

Setting Rotation Interval

5

■ Log File Location

→ To see where the log files stored

/home/user-name/.pm2/logs 
6 ■ Reference https://www.npmjs.com/package/pm2-logrotate 

PM2 Version Update 

The purpose of updating the PM2 version is to ensure that the commands for restart strategies function correctly.

S. No

Task

Command

1

■ Check PM2 version

→ Ensure the PM2 version is 5.4.2 or above.

→ If not, update the PM2 version using the below commands 2,3,4. 

Check PM2 version
2

■ Backup Process List:  

→ Before installing the latest pm2, it’s a good idea to save your current process list.  

→ Running pm2 save is important because it saves the current process list. 

→ This way, if you restart your server or PM2 itself, you can easily restore all your processes to their previous state using pm2 resurrect. 

→ Essentially, it ensures that your processes are not lost and can be quickly reloaded, maintaining your application’s uptime and stability. 

Backup Process List
3

■ Install Latest PM2 

→ Use npm to install the latest version of PM2 globally 

 

4

■ Update the In-Memory PM2 Daemon 

→ After installing the latest version, update the in-memory PM2 daemon.  

→ Only after this update command the features of pm2 installed will work. 

 

 
5 Reference https://pm2.keymetrics.io/docs/usage/update-pm2/ 

PM2 Restart strategies

This will restart the application servers at pre-defined frequencies. 

S. No

Task

Command

1

■ Set up the cron restart 

→ Schedule a restart at midnight every day.  

→ Adjust the cron parameters as needed.

 

Set up the cron restart 
2

■ Restart process and disable cron restart

→ Disable any cron-based restart schedule

 
3

■ Check the cron restart period

→  Verify the cron restart configuration for a process

 

Check the cron restart period
4 ■ Reference  https://pm2.keymetrics.io/docs/usage/restart-strategies/ 

Conclusion

Learning PM2 is essential for any Node.js developer aiming to optimize application performance and reliability. By learning how to configure log rotation, keep PM2 updated, and implement effective restart strategies, you’ll ensure your applications run smoothly and efficiently. These techniques not only enhance stability but also simplify maintenance and troubleshooting, making your server environments more robust.

Follow Us On

Registered Office

CHG IT CONSULTANCY PVT LTD

STPI Technology Incubation Centre,
2nd Floor, No.5, Rajiv Gandhi Salai,
Taramani, Chennai – 600113,
Tamil Nadu, INDIA

Parent Office

CIC Corporation

2-16-4 Dogenzaka, Shibuya-ku,
Nomura Real Estate,
Shibuya Dogenzaka Building,
Tokyo 150-0043, JAPAN

  +81 03-3496-1571
AboutUs

CHG IT Consultancy Pvt. Ltd. is a subsidiary of CIC Holdings Co. Ltd. Japan. Our company is focused on IT related solutions to reap the benefits of global popularity of Software Industry.

Registered Office
CHG IT CONSULTANCY PVT LTD

STPI Technology Incubation Centre,
2nd Floor, No.5, Rajiv Gandhi Salai,
Taramani, Chennai – 600113,
Tamil Nadu, INDIA

CIC Corporation

2-16-4 Dogenzaka, Shibuya-ku,
Nomura Real Estate,
Shibuya Dogenzaka Building,
Tokyo 150-0043, JAPAN

+81 03-3496-1571