Wait for mongodb to be ready before starting pm2 process on reboot

不羁的心 提交于 2020-01-01 16:39:10

问题


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)

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

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.

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

Note: I tried adding

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

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


回答1:


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.

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

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

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

pm2 status

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

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


来源:https://stackoverflow.com/questions/51231278/wait-for-mongodb-to-be-ready-before-starting-pm2-process-on-reboot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!