Check if process is running, if not start it
I tried a lot of solutions including with fsockopen()
to check if port is open...
But the only working solution is to check if node js application process is on with for argument the application file. If not I use exec
to run it permanently (with &
argument) and putting output in a logfile (in the same folder of the node app).
$node = '/usr/bin/node';$base = '/path/to/your/app';$appfile = 'my_app.js';$logfile = 'my_app.log';$proc = explode("\n", trim(shell_exec(" ps -ef | grep -v grep | grep $appfile")));if ($proc[0] == '') { exec("$node $base/$appfile >> $base/$logfile &");}
It's working fine and running the node server only once.
EDIT : Minimalist PHP code version :
if(trim(shell_exec('ps -ef | grep -v grep | grep app.js')))=='') { exec('node /path/to/app.js >> /path/to/app.log &');}