Cheetara
Cheetara

Reputation: 539

pm2 --watch is logging every 3 seconds irrespective of config file

We have the below config file for pm2:

module.exports = {
  apps: [
    {
      script: 'index.js',
      // ------------------------------------ watch options - begin
      watch: ['/testing'],
      watch_delay: 10000,
      ignore_watch: ['node_modules', 'logs'],
      watch_options: {
        followSymlinks: false,
      },
      // ------------------------------------ watch options - end
      error_file: 'logs/err.log',
      out_file: 'logs/out.log',
      log_file: 'logs/combined.log',
      time: true,
    },
  ],

  deploy: {
    production: {
      user: 'SSH_USERNAME',
      host: 'SSH_HOSTMACHINE',
      ref: 'origin/master',
      repo: 'GIT_REPOSITORY',
      path: 'DESTINATION_PATH',
      'pre-deploy-local': '',
      'post-deploy':
        'npm install && pm2 reload ecosystem.config.js --env production',
      'pre-setup': '',
    },
  },
};

With this in place, and a single index.js file:

console.log(`testing`);

We get 'testing printed to the log file every 3 seconds;

2021-05-31T12:02:39: testing
2021-05-31T12:02:42: testing
2021-05-31T12:02:45: testing
2021-05-31T12:02:48: testing
2021-05-31T12:02:51: testing
2021-05-31T12:02:55: testing

There are no changes to the files, and this isn't the logs directory or files being monitored as they're excluded with ignore_watch: ['node_modules', 'logs'],.

Why isn't --watch only monitoring for file changes, and instead logging every 3 seconds?

Upvotes: 0

Views: 929

Answers (1)

RickN
RickN

Reputation: 13500

PM2 is detecting that your app is exiting and restarting your app.

You can choose what PM2 should do when it detects when your app exists, such as --no-autorestart.


Example log output:

$ pm2 --watch --no-autorestart --ignore-watch=node_modules start index.js
$ pm2 logs -f

PM2        | App name:index id:0 online
0|index    | testing
PM2        | App [index] with id [0] and pid [48907], exited with code [0] via signal [SIGINT]

# Modify index.js to log `testing 2`.

PM2        | Change detected on path index.js for app index - restarting
PM2        | App name:index id:0 online
0|index    | testing 2
PM2        | App [index] with id [0] and pid [48910], exited with code [0] via signal [SIGINT]

Upvotes: 1

Related Questions