Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

php - trigger node js file

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...