删除 PM2 后无法重新启动 Node.js 应用

Unable to restart Node.js app after PM2 delete

我通常使用 pm2 stop 来停止我的应用程序,它工作正常。

然而,当我刚刚在我的应用程序上尝试 pm2 delete 并再次启动它时,它似乎不再起作用了。

这是我检索到的pm2错误日志。没有更改任何代码,所以我认为突然关闭可能会导致一些 ECONNRESET 错误? (没有任何清理代码)

当我使用 ps -efpm2 kill 发现它时,也尝试杀死 pm2 和 mongod 的 pid 并重新启动 mongod 无济于事。

Error: Invalid mongodb uri. Must begin with "mongodb://"
  Received: NaN
    at muri (/home/ubuntu/portal/node_modules/muri/lib/index.js:28:11)
    at NativeConnection.Connection.openUri (/home/ubuntu/portal/node_modules/mongoose/lib/connection.js:713:18)
    at Mongoose.createConnection (/home/ubuntu/portal/node_modules/mongoose/lib/index.js:198:17)
    at Object.<anonymous> (/home/ubuntu/portal/models/stopwords.js:11:19)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Module.require (module.js:517:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/ubuntu/portal/helpers/database/helper_mongodb.js:2:19)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Module.require (module.js:517:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/ubuntu/portal/controllers/inf_controller.js:2:19)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)

stopwords.js:11:19 的代码是

let db = mongoose.createConnection(process.env.MONGODB_URI + process.env.MONGODB_HELPER_DB, { useMongoClient: true })

正如我们评论的那样,正如您所做的那样,我也使用 pm2 解决了我的问题 + g运行t 到 运行 我的 express.js 应用程序。将尝试在这里解释关闭问题的解决方案。

The way to start and stop pm2 with different apps and without trouble was with an ecosystem file where I configured my node apps.(centOS apache)

我的生态系统文件

-bash-4.2$ pm2 ecosystem
File /var/www/vhosts/domain.com/ecosystem.config.js
[...]
-bash-4.2$ cat ecosystem.config.js 
module.exports = {
  /**
   * Application configuration section
   * http://pm2.keymetrics.io/docs/usage/application-declaration/
   */
  apps : [
    // First application
    {
      name      : 'DomainProd',
      cwd       : '/var/www/vhosts/domain.com/httpdocs/App',
      script    : 'start.sh'
    },
   {
      name      : 'DomainTest',
      cwd       : '/var/www/vhosts/domain.com/testing.domain.com/httpdocs/App',
      script    : 'start-test.sh'  
    }
  ]
};

start.sh脚本

-bash-4.2$ cat /var/www/vhosts/domain.com/httpdocs/App/start.sh 
/usr/bin/grunt serve:prod
-bash-4.2$

我的 G运行tfile

上的服务任务
return grunt.task.run(['concurrent:cleanandbuild', 'build', 'env:all', 'env:prod', 'express:prod', 'wait', 'express-keepalive']);

运行例子:

-bash-4.2$ pm2 status
┌────────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬────────────┬──────────┐
│ App name   │ id │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem        │ watching │
├────────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼────────────┼──────────┤
│ DomainProd │ 0  │ fork │ 17992 │ online │ 63      │ 20D    │ 0%  │ 264.1 MB   │ disabled │
│ DomainTest │ 1  │ fork │ 25526 │ online │ 295     │ 113m   │ 0%  │ 1.2 MB     │ disabled │
└────────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴────────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
-bash-4.2$ pm2 stop 1
[PM2] Applying action stopProcessId on app [1](ids: 1)
[PM2] [Domainest](1) ✓
┌────────────┬────┬──────┬───────┬─────────┬─────────┬────────┬─────┬────────────┬──────────┐
│ App name   │ id │ mode │ pid   │ status  │ restart │ uptime │ cpu │ mem        │ watching │
├────────────┼────┼──────┼───────┼─────────┼─────────┼────────┼─────┼────────────┼──────────┤
│ DomainProd │ 0  │ fork │ 17992 │ online  │ 63      │ 20D    │ 0%  │ 264.1 MB   │ disabled │
│ DomainTest │ 1  │ fork │ 0     │ stopped │ 295     │ 0      │ 0%  │ 0 B        │ disabled │
└────────────┴────┴──────┴───────┴─────────┴─────────┴────────┴─────┴────────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
-bash-4.2$ pm2 start 1
[PM2] Applying action restartProcessId on app [1](ids: 1)
[PM2] [DomainTest](1) ✓
[PM2] Process successfully started
┌────────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬────────────┬──────────┐
│ App name   │ id │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem        │ watching │
├────────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼────────────┼──────────┤
│ DomainProd │ 0  │ fork │ 17992 │ online │ 63      │ 20D    │ 0%  │ 264.1 MB   │ disabled │
│ DomainTest │ 1  │ fork │ 29816 │ online │ 295     │ 0s     │ 0%  │ 1.2 MB     │ disabled │
└────────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴────────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
-bash-4.2$ pm2 logs 1
[TAILING] Tailing last 15 lines for [1] process (change the value with --lines option)
/var/www/vhosts/domain.com/.pm2/logs/DomainTest-out-1.log last 15 lines:
1|DomainTe |     Execution Time (2017-10-02 19:33:24 UTC+2)
1|DomainTe |     loading tasks  579ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 92%
1|DomainTe |     clean:server    47ms  ▇▇▇▇ 7%
1|DomainTe |     Total 632ms
1|DomainTe | 
1|DomainTe |     Running "clean:dist" (clean) task
1|DomainTe |     >> 5 paths cleaned.
1|DomainTe | 
1|DomainTe |     Done, without errors.
1|DomainTe | 
1|DomainTe | 
1|DomainTe |     Execution Time (2017-10-02 19:33:24 UTC+2)
1|DomainTe |     loading tasks  570ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 55%
1|DomainTe |     clean:dist     456ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 44%
1|DomainTe |     Total 1s

[STREAMING] Now streaming realtime logs for [1] process

希望它能帮助到这里的任何人。