我们有以下 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': '',
},
},
};
有了这个,还有一个 index.js
文件:console.log(`testing`);
我们每 3 秒将 'testing 打印到日志文件中;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
文件没有变化,这不是 logs
正在监视的目录或文件,因为它们被 ignore_watch: ['node_modules', 'logs'],
排除在外.为什么不是
--watch
只监视文件更改,而是每 3 秒记录一次?
最佳答案
PM2 正在检测您的应用程序正在退出并重新启动您的应用程序。
您可以choose what PM2 should do when it detects when your app exists ,如 --no-autorestart
.
示例日志输出:
$ 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]
关于node.js - pm2 --watch 每隔 3 秒记录一次,与配置文件无关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67774159/