簡體   English   中英

刪除PM2后無法重新啟動Node.js應用

[英]Unable to restart Node.js app after PM2 delete

我通常會執行pm2 stop來停止我的應用程序,並且工作正常。

但是,當我剛剛嘗試在應用程序上刪除pm2並再次啟動它時,它似乎不再起作用。

這是我檢索的pm2錯誤日志。 沒有代碼被更改,所以我認為突然關機可能是某些ECONNRESET錯誤? (沒有任何清理代碼)

當我發現使用ps -ef ,也嘗試殺死pm2和mongod的pid,並且還嘗試pm2 kill並重新啟動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 + 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上的serve任務

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

希望它能幫助任何人到達這里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM