duananyu9231 2011-12-23 21:07
浏览 28
已采纳

触发节点js文件

I have a JS file that runs via node.js, so normally I crank open a terminal window and type something like node myfile.js and it runs and runs all day; fun.

Then when I want to reboot it, I hit control-c and and it quits. Then I can run the command again.

Now what I would like to do is be able to do this through a web page so that my users can run specific JS files and also be able to "reboot" them.

So two questions about this:

  1. Is it possible, and if so how do I get started?
  2. Is it safe, and if not, can it be made safe?

Based on swatkins response

i tried this and got nothing

 $output = exec("node -v");
 echo $output;

then i tried on my local machine and it worked fine what gives ?

also I'm still not sure about the security and the ability to stop a running script

  • 写回答

2条回答 默认 最新

  • duanfu1873 2011-12-23 21:21
    关注

    Assuming you're on Unix-based OS:

    You can run shell commands via the exec() function:

    // in php file
    // to start the script 
    exec("node myscript.js &", $output);
    

    $output becomes an array of each line of output, so you can see what the process id is. Then you would use that process id to kill the script:

    exec("kill " . $processid);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)