触发节点js文件 [英] trigger node js file
问题描述
我有一个JS文件,通过node.js运行,所以通常我打开一个终端窗口并键入类似 node myfile.js
,它运行并运行所有天;有趣。
然后当我要重新启动它时,我按下control-c,然后退出。然后我可以再次运行命令。
现在我想做的是能够通过一个网页,这样我的用户可以运行特定的JS文件和也可以重新启动他们。
有两个问题:
- 是否安全?如果没有,是否安全?
- Is it possible, and if so how do I get started?
- Is it safe, and if not, can it be made safe?
<
ol>
基于swatkins回应
我试过这个,没有任何东西
$ output = exec(node -v);
echo $ output;
然后我在我的本地机器上尝试了,它工作得很好吗?
我仍然不确定安全性和停止运行脚本的能力
假设您使用的是基于Unix的操作系统:
您可以通过exec()函数运行shell命令:
//在php文件中
//启动脚本
exec(node myscript.js&,$ output);
$ output
的输出,所以你可以看到进程id是什么。然后你将使用进程id来杀死脚本:
exec(kill。
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:
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
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);
这篇关于触发节点js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!