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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条