Search code examples
node.jspm2

How to get pm2 process to watch after it has been stopped/ restarted?


My pm2 process starts using their default ecosystem file structure:

ecosystem.config.js

module.exports = {
    apps: [{
        env: {
            NODE_ENV: "development"
        },
        error_file: "./logs/error.log",
        ignore_watch: ["logs", "node_modules"],
        log_date_format: "YYYY-MM-DD HH:mm:ss Z",
        name: "my-app",
        out_file: "./logs/output.log",
        script: "./server.js",
        watch: true
    }]
}

I start the process with pm2 start ecosystem.config.js and that works fine, with the app reloading on file changes.

But when I stop the process with pm2 stop ecosystem.config.js, and then start it again with pm2 start ecosystem.config.js, pm2 does not watch for files, despite the display column of watching being enabled.

The only way to start the process up again and have the watch work is to delete the pm2 process, and then start up a new one again.

Am I missing something to make a stop or restart work with watch?

Thanks.


Solution

  • The pm2 watch & restart documentation had the answer (must have glossed over it on first read):

    Restart with --watch will toggle the watch parameter.

    Looks like omitting that --watch flag on already-existing pm2 instances will not toggle the watch parameter in the ecosystem.config.js file. The watch parameter is only toggled on initial process execution, not subsequent ones.

    So stopping the process, then starting again with pm2 start ecosystem.config.js --watch does the trick!