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

时间:2017-09-13 11:43:39

标签: node.js mongodb express mongoose pm2

我通常会在pm2停止停止我的应用程序并且工作正常。

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

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

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

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 })

1 个答案:

答案 0 :(得分:0)

正如我们评论的那样,我也使用 pm2解决了我的问题  + grunt 来运行我的express.js应用。将尝试在此解释解决问题的解决方案。

  

使用不同的应用程序启动和停止pm2的方法,没有任何问题   有一个生态系统文件,我配置了我的节点应用程序。(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$

我的Gruntfile上的服务任务

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

希望能帮助到达这里的人。