Deployment using PM2 without downtime.

suggest change

ecosystem.json

{
    "name": "app-name",
    "script": "server",
    "exec_mode": "cluster",
    "instances": 0,    
    "wait_ready": true,
    "listen_timeout": 10000,
    "kill_timeout": 5000,
}
wait_ready

Do not treat the process as ready on the listening event. Wait until it calls process.send('ready'). process.send exists only when PM2 started the process.

listen_timeout

Time in ms before forcing a reload if app not listening.

kill_timeout

Time in ms before sending a final SIGKILL.

server.js

const http = require('http');
const express = require('express');

const app = express();
const server = http.createServer(app);
const port = 80; // binding to port 80 usually requires extra privileges

server.listen(port, function() {
    if (process.send) process.send('ready');
});

process.on('SIGINT', function() {
    server.close(function() {
        process.exit(0);
    });
});

You may need to wait until the application has established connections to databases, caches, or workers. With wait_ready: true in the process file, PM2 waits for process.send('ready') before treating the process as online.

When PM2 stops or restarts a process, it sends signals in order. First it sends SIGINT, which the process can catch to finish in-flight work. If the process is still running after kill_timeout (1600 ms unless you set it; this file uses 5000), PM2 sends SIGKILL. Catch SIGINT when the application needs to clean up state or jobs before it exits.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents