[英]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.