Pm2: CTRL-C graceful exit vs PM2 stop under Windows

Created on 7 May 2018  ยท  10Comments  ยท  Source: Unitech/pm2

What's going wrong?

When I press CTRL-C while running my NodeJS under Windows 10 64 bits, my shutdown sequence executes properly. But on the other hand, using pm2 stop doesn't call the shutdown sequence at all.

How could we reproduce this issue?

Run a NodeJS program with process.on("SIGINT") under Windows.
I am also monitoring for SIGBREAK, SIGUSR2 and SIGTERM.

        // Constants
        this.signals = [
            "SIGBREAK",
            "SIGINT",
            "SIGUSR2",
            "SIGTERM"
        ];
        // IPC signals
        this.signals.forEach((signal) => {
            this.debug("Adding hook on signal " + signal);

            process.once(signal, () => {
                this.debug(signal + " received");
                this.exit(signal);
            });
        });

I also have this snippet in the code:

        // NodeJS events
        process.on("message", (msg) => {
            this.dump(msg, "MSG");

            switch (msg) {
                case "shutdown":
                    this.exit(msg);
                    break;
            }
        });

But it is never called.

Also this one found on Stackoverflow:

        // Windows process signals IPC fallback code
        if (process.platform === "win32") {
            this.log("Running on the Windows 32 platform");

            let rl = require("readline").createInterface({
              input: process.stdin,
              output: process.stdout
            });

            this.signals.forEach((signal) => {
                this.debug("Forwarding win32 signal " + signal + " to NodeJS");

                rl.on(signal, () => {
                    this.debug("Received win32 signal " + signal);
                    process.emit(signal);
                });
            });
        }

Supporting information

===============================================================================
--- PM2 REPORT (Mon May 07 2018 10:30:34 GMT-0400 (Eastern Summer Time)) ------
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 2.10.2
node version         : 8.9.1
node path            : C:/Program Files/nodejs/node
argv                 : C:\Program Files\nodejs\node.exe,C:\Users\masse\AppData\Roaming\npm\node_modules\pm2\lib\Daemon.js
argv0                : node
user                 : undefined
uid                  : N/A
gid                  : N/A
uptime               : 15min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 2.10.2
node version         : 8.9.1
node path            : undefined
argv                 : C:\Program Files\nodejs\node.exe,C:\Users\masse\AppData\Roaming\npm\node_modules\pm2\bin\pm2,report
argv0                : node
user                 : masse
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : win32
type                 : Windows_NT
cpus                 : Intel(R) Core(TM) i5-2500 CPU @ 3.30GHz
cpus nb              : 4
freemem              : 2841792512
totalmem             : 8553111552
home                 : C:\Users\masse
===============================================================================
--- PM2 list -----------------------------------------------
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Name     โ”‚ mode โ”‚ status โ”‚ โ†บ โ”‚ cpu โ”‚ memory    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ rest-api โ”‚ fork โ”‚ online โ”‚ 3 โ”‚ 0%  โ”‚ 61.8 MB   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
===============================================================================
--- Daemon logs --------------------------------------------
C:\Users\masse\.pm2\pm2.log last 20 lines:
PM2        | [2018-05-07 10:15:23] PM2 log: ===============================================================================
PM2        | [2018-05-07 10:15:23] PM2 log: [Watch] Start watching
rest-api
PM2        | [2018-05-07 10:15:23] PM2 log: Starting execution sequence in -fork mode- for app name:rest-api id:0
PM2        | [2018-05-07 10:15:23] PM2 log: App name:rest-api id:0
online
PM2        | [2018-05-07 10:16:56] PM2 log: Stopping app:rest-api id:0
PM2        | [2018-05-07 10:16:56] PM2 log: App [rest-api] with id
[0] and pid [21960], exited with code [1] via signal [SIGINT]
PM2        | [2018-05-07 10:16:56] PM2 log: pid=21960 msg=process killed
PM2        | [2018-05-07 10:16:56] PM2 log: Starting execution sequence in -fork mode- for app name:rest-api id:0
PM2        | [2018-05-07 10:16:56] PM2 log: App name:rest-api id:0
online
PM2        | [2018-05-07 10:17:25] PM2 log: Stopping app:rest-api id:0
PM2        | [2018-05-07 10:17:26] PM2 log: App [rest-api] with id
[0] and pid [30500], exited with code [1] via signal [SIGINT]
PM2        | [2018-05-07 10:17:26] PM2 log: pid=30500 msg=process killed
PM2        | [2018-05-07 10:17:26] PM2 log: Starting execution sequence in -fork mode- for app name:rest-api id:0
PM2        | [2018-05-07 10:17:26] PM2 log: App name:rest-api id:0
online
PM2        | [2018-05-07 10:26:32] PM2 log: Process 0 in a stopped
status, starting it
PM2        | [2018-05-07 10:26:32] PM2 log: Stopping app:rest-api id:0
PM2        | [2018-05-07 10:26:32] PM2 log: App [rest-api] with id
[0] and pid [28584], exited with code [1] via signal [SIGINT]
PM2        | [2018-05-07 10:26:32] PM2 log: pid=28584 msg=process killed
PM2        | [2018-05-07 10:26:32] PM2 log: Starting execution sequence in -fork mode- for app name:rest-api id:0
PM2        | [2018-05-07 10:26:32] PM2 log: App name:rest-api id:0
online
stale

Most helpful comment

Same here SIGTERM and message does not work Ctrl-C does.

All 10 comments

I don't see how this commit is related. Any comments on this issue?

Same here SIGTERM and message does not work Ctrl-C does.

I've found exactly the same, first coming across this issue https://github.com/Unitech/pm2/issues/3691. I've tried multiple things and I don't get a SIGINT or SIGKILL message, and the process.on("message") also doesn't get called.

Makes it virtually impossible to shutdown gracefully in windows.

I am experiencing the same issue as @IPWright83 plus a bunch of others.
BTW... PM2 Runtine | Best Practices | Windows Graceful Stop does not solve the issue!

I experienced this same issue a year or more ago.
I upgraded PM2 to 3.3 (from 2.x), in the hope it would have been resolved by now... seems not ๐Ÿ‘Ž

Also, I noticed all pm2 logs will be running background..

Bump

Any workaround?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I'm not sure closing this as stale is the right action here.

Facing the same issue...Pls suggest how to terminate the pm2 process with control + c

Was this page helpful?
0 / 5 - 0 ratings

Related issues

webchaz picture webchaz  ยท  3Comments

rangercyh picture rangercyh  ยท  4Comments

alexpts picture alexpts  ยท  3Comments

phra picture phra  ยท  3Comments

lefam picture lefam  ยท  3Comments