duanmengmiezen8855 2015-12-08 10:11
浏览 489

通过从NodeJS中执行PHP脚本来重新启动NodeJS

I have a problem trying to update a folder and deploy its contents again. I have to use NodeJS and have gotten port 8080 to work with. I have tried to create a php script (update.php):

<?php
echo exec("git pull");
echo exec("node app.js");
?>

Now i want to start the NodeJS script to update and the ultimate goal is to be able to restart the server.

I use the express module:

app.get('/Update', function(req,res) {
   exec("php update.php", function(error, stdout, stderr) {
      process.exit(0);
   }
}

The problem is that the NodeJS server quits when it gets a response from the script but the script tries to start the NodeJS server. This obviously cannot happen since it is already running on the specified port.

Now i found on google to use the module called 'nodemon' however I am not given sudo access so installing this is out of the question. Other results are using

ps aux | grep node
kill -9 PROCESS_ID

This also yields problems since it is hard to configure the PHP script to kill the specified process but aside from that there are other NodeJS servers running in other child folders of the parent folder I am given. This means that if I'd use 'killall node' I'd get a lot of people angry that I killed their servers.

What is the best approach to solving this problem using only port 8080 and wanting to deploy the changes in the Github repo when accessing a certain link?

  • 写回答

2条回答

  • dougaicha5258 2015-12-08 10:35
    关注

    You can get the node's process id by Node.js command process.pid.

    You can then make a bash script that executes all your commands and give the PID in environment variable.

    1) Forget the update.php

    2) route

    app.get('/update', function(req, res) {
      var spawn = require('child_process').spawn;
      spawn('sh', [ 'update.sh' ], {
        env: { NODE_PID: process.pid }
      });
    });
    

    3) update.sh

    git pull
    kill -9 $NODE_PID
    node app.js
    
    评论

报告相同问题?

悬赏问题

  • ¥60 Python如何后台操作Vmwake虚拟机键鼠
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容