等待mongodb准备就绪,然后在重新启动时启动pm2进程 [英] Wait for mongodb to be ready before starting pm2 process on reboot

查看:297
本文介绍了等待mongodb准备就绪,然后在重新启动时启动pm2进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让pm2等待mongodb就绪,然后再启动系统重新启动过程. (我正在使用Ubuntu 16.04服务器)

I am having trouble making pm2 wait for mongodb to be ready before it starts a process on system reboot. (I am using Ubuntu 16.04 server)

在我的pm2的systemd服务文件中,有这个文件,我认为它将等到mongodb启动之后:

In my systemd service file for pm2 I have this, which I thought would make it wait until after mongodb was started:

[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
Wants=mongod.service
After=network.target mongod.service

[Service]
Type=forking

但是似乎它运行pm2,因此在mongodb实际准备好监听27017之前,node.js处理pm2启动.我的错误日志显示,我的node.js脚本找不到27017上的数据库. .但是,如果我手动重新启动该过程(在mongodb有足够的时间准备好之后),它将很好用.

But it seems that it runs pm2, and therefore the node.js processes that pm2 launches before the mongodb actually is ready to listen on 27017. My error logs show that my node.js script can't find the db on 27017. But if I manually restart the process (after mongodb has had plenty of time to get ready) it works just great.

如何将pm2启动延迟,或将pm2启动特定应用延迟到 mongodb准备就绪并侦听连接之后?

How can I delay pm2 starting, or delay pm2 starting a particular app until after mongodb is ready and listening for connections?

注意:我尝试添加

ExecStartPost=/bin/sh -c 'while ! /usr/bin/mongo --eval "db.version()" > /dev/null 2>&1; do sleep 0.1; done'

按照以下答案转到我的mongod.service:系统:mongodb之后的自动启动服务,但这完全阻止了pm2的启动.

To my mongod.service as per this answer: Systemd: Autostart service after mongodb, but that just prevented pm2 from starting at all.

推荐答案

我刚遇到了这个完全相同的问题,而最终找到的解决方案是让pm2也启动mongodb.只要在启动npm之前先启动mongodb,然后在让pm2生成启动脚本之前保存当前进程列表,一切都会很好.

I just ran into this exact same issue and the solution I finally found was to have pm2 also start mongodb. As long as you start mongodb before you start npm and then save the current process list before having pm2 generate the startup script, everything works great.

因此,要使pm2运行mongodb,您需要创建一个可以运行pm2的shell脚本.这是我的mongod.sh文件的样子:

So, to get pm2 to run mongodb, you need to create a shell script that pm2 can run. This is what my mongod.sh file looks like:

#!/bin/bash
sudo mongod

我将那个mongod.sh文件放在我的用户目录中,然后像这样用pm2启动它和npm:

I put that mongod.sh file in my user directory and then started it and npm with pm2 like so:

pm2 start ~/mongod.sh
pm2 start npm

通过运行,您可以检查以确保两个脚本都在pm2上以正确的顺序运行

You can check to make sure both scripts are running with pm2 and in the right order by running

pm2 status

在此过程中,您应该看到mongod和npm都在运行,并且mongod的id应该为0(或刚好低于npm过程).

In the process, you should see both mongod and npm running and the id for mongod should be 0 (or just lower than the npm process).

一旦使mongod和npm都与pm2一起运行,就保存了pm2进程列表并重置启动脚本:

Once I had both mongod and npm running with pm2, I saved the pm2 process list and reset the startup script:

pm2 save
pm2 unstartup
pm2 startup

这篇关于等待mongodb准备就绪,然后在重新启动时启动pm2进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆