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
To run a Node. js script from within another Node. js script with JavaScript, we can use the child_process module's fork function. require("child_process").
js provides an inbuilt module called FS (File System). Node. js gives the functionality of file I/O by providing wrappers around the standard POSIX functions. All file system operations can have synchronous and asynchronous forms depending upon user requirements.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With