Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js pm2 delay script launch

I use the awesome pm2 package to keep my node.js apps alive, but i have an issue I don't know how to fix.

One of my apps requires several scripts, a server and a few clients. Often when I restart my server, all the script are restarted, but connection isn't established because the clients have loaded before the server. Is it possible to make sure that a script starts after the other have loaded? Let me try to rephrase so it gets clearer

I have :

pm2 start server.js
pm2 start client.js

And I'd like to somehow start the client only when the server is started.

Apologies is my question seems unclear, please comment and I'll explain more!

like image 515
xShirase Avatar asked Feb 01 '14 17:02

xShirase


People also ask

Does PM2 auto restart on file change?

js App in Development Mode with PM2. At this point, your server will auto-restart each time you create, modify or delete a source file in your project. It also works when you add or remove a dependency with npm or yarn .

Why does PM2 keep restarting?

PM2 allows to reload (auto fallback to restart if not in cluster) an application based on a memory limit/ Please note that the PM2 internal worker (which checks memory), starts every 30 seconds, so you may have to wait a bit before your process gets restarted automatically after reaching the memory threshold.


2 Answers

Run Pm2 using this script

pm2 start node <start script> --restart-delay <time in milliseconds>
like image 152
Rahul Singh Avatar answered Oct 07 '22 22:10

Rahul Singh


Rewrite your client to make several connection attempts. It is always a good thing to do, and it'll also help in this case.

like image 22
alex Avatar answered Oct 07 '22 20:10

alex