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.