Manage Application With PM2

Last Updated Date: 24-06-2023

This note outlines the way to setup PM2 for the application. Which can be used to run the application in the background and restart it when needed.

Introduction of PM2

PM2 is a helpful tool for Node.js applications. It ensures continuous operation of apps and automatically restarts them if they crash. Additionally, PM2 provides centralized management for all apps, ensuring their availability to users.

Installation

To install PM2 globally, follow the command below:

# npm
npm install -g pm2

# pnpm
pnpm add -g pm2

Commands

There are a lot of commands for PM2 to controll the application.

Start / Stop / Restart / Delete

To begin running an application (e.g., app.js) using PM2, follow these steps:

Start the application with this command:

pm2 start app.js --name app

Once started, PM2 will automatically handle the application named app as a background process and restart it if it crashes.

To stop, restart, or delete the app, use the corresponding commands:

pm2 stop app

pm2 restart app

pm2 delete app

Please be aware that the delete command removes the app from PM2's list, and after executing it, the stop and restart commands won't work anymore. To start the app again, please to use the start command.

List

After starting several applications, the list command can be used to view all the applications managed by PM2. It displays their names, the number of restarts, their uptime, and various status indicators

pm2 list

Monitor

To monitor all the PM2 application, monit command can be used as it opens a real-time monitoring interface that display crucial information about the running applications. Including CPU and memory usage, number of instances, and logs etc.

pm2 monit

Configuration file

To customize the settings, it is recommended to create a configuration file for better readability.

To do this, simply make a file called ecosystem.config.js in the root folder of the project and put all the configurations there.

Example for normal node application:

module.exports = {
    apps: [
        {
            // basic
            name: "node-app", // name of the app
            script: "app.js", // script location
            instances: 1, // launch more for load balancing
            exe_mode: "cluster", // to use multi-thread
            watch: false, // watch file changes
            // log
            out_file: "./pm2-out.log",
            error_file: "./pm2-err.log",
            time: true, // show time in log
            // restart
            autorestart: true, // auto restart
            restart_delay: 5000, // short delay may cause issue for Next.js
            max_memory_restart: "4000M", // maximum memory to use
        },
    ],
};

Example of single-page application (SPA):

module.exports = {
    apps: [
        {
            // basic
            name: "node-app",
            script: "serve", // serve as SPA
            instances: 1,
            exe_mode: "cluster",
            watch: false,
            env: {
                PM2_SERVE_PATH: "dist", // path to dist/build
                PM2_SERVE_PORT: 2000, // server port
                PM2_SERVE_SPA: "true", // spa
                PM2_SERVE_HOMEPAGE: "/index.html", // index
            },
            // log
            out_file: "./pm2-out.log",
            error_file: "./pm2-err.log",
            time: true,
            // restart
            autorestart: true,
            restart_delay: 5000,
            max_memory_restart: "4000M",
        },
    ],
};

Example for Next.js application:

module.exports = {
    apps: [
        {
            // basic
            name: "example",
            script: "node_modules/next/dist/bin/next", // location of command "next"
            args: "start -p 4000", // arguments for Next.js
            instances: 1,
            exec_mode: "cluster",
            watch: false,
            // log
            out_file: "./pm2-out.log",
            error_file: "./pm2-err.log",
            time: true,
            // restart
            autorestart: true,
            listen_timeout: 10000, // timeout to mark it as crash
            kill_timeout: 1000, // timeout to shutdown the app
            restart_delay: 10000,
            max_memory_restart: "4000M",
        },
    ],
};

Conclusion

In conclusion, PM2 simplifies application deployment and maintenance while also accommodating various application types beyond just JavaScript, making it a versatile and reliable process manager for diverse projects.

Feel free to contact if there are any problem in this note.