PM2,以代码0退出(在启动时重新启动)

时间:2020-03-17 11:33:15

标签: node.js docker pm2

我试图在我的应用程序上安装PM2,它在localhost上运行良好。但是,当我将应用程序移至docker容器并尝试在Linux机器上运行时,会发生问题。

我正在运行命令npm run production,该命令执行以下代码:

pm2 start ecosystem.config.js --env production --no-autorestart

此后,在命令行上,我看到了PM2的启动方式并在所有可用内核上启动了我的应用程序。

app         | [PM2] PM2 Successfully daemonized
app         | [PM2][WARN] Applications App not running, starting...
app         | [PM2] App [App] launched (2 instances)
app         | ┌─────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
app         | │ id  │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
app         | ├─────┼───────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
app         | │ 0   │ App    │ default     │ 1.0.0   │ cluster │ 36       │ 0s     │ 0    │ online    │ 0%       │ 38.0mb   │ root     │ disabled │
app         | │ 1   │ App    │ default     │ 1.0.0   │ cluster │ 43       │ 0s     │ 0    │ online    │ 0%       │ 25.0mb   │ root     │ disabled │
app         | └─────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

然后它返回一条消息:

app exited with code 0

整个过程一次又一次地陷入困境... 我尝试在“ fork”上的“ cluster_mode”上运行它,我尝试使用--no-autorestart运行它,但它仍然不断重启。

我真的不知道我在做什么错?

@@ EDIT,添加了ecosystem.config.js


module.exports = {
  apps : [{
    name: 'App',
    script: './build/server/index.js',

    // Options reference: https://pm2.keymetrics.io/docs/usage/application-declaration/
    exec_mode: 'cluster_mode',
    args: 'none',
    instances: os.cpus().length,
    autorestart: false,
    watch: false,
    // max_memory_restart: '3G',
    env: {
      NODE_ENV:'development',

    },
    env_production: {
      NODE_ENV:'production',

    }
  }],

  deploy : {
    production : {
      user : 'szygendab',
      host : '212.83.163.1',
      ref  : 'origin/master',
      repo : 'git@github.com:repo.git',
      path : '/var/www/production',
      'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
    }
  }
};

2 个答案:

答案 0 :(得分:0)

更新

我找到了解决方案。基本上,docker上的应用程序不应从pm2开始运行,而应从pm2-runtime开始运行。这就是为什么它在localhost而不是在docker映像内工作的原因。据我了解,docker完成了一个任务并关闭了它,此后它继续接收另一个启动任务,并正在循环启动该应用程序。

相反,pm2 start...。我将测试实例的方法切换为pm2-runtime start ...,它很有帮助。

答案 1 :(得分:0)

就我而言,我有几个 PM2 应用程序运行良好,但转移到我不熟悉的新文件结构。 npm start 总是可以正常工作,因为它指向正确的文件。在我的情况下,正确的文件意味着那里有一行代码告诉服务器侦听端口。我假设我的新文件结构和旧文件结构一样(因为我有其他业务要运行,所以我离开了几年的开发)。我告诉 PM2 在根服务器目录中启动 app.js,但在 app.js 中没有调用实际运行服务器。非常大,但是当你生疏并想要开始一个新项目时,这就是这些错误发生的时候。为了简化事情,我在服务器根目录中使用了一个 process.json 文件并指向了相应的文件。就我而言,它是这样写的:

enter image description here