Sunday, 7 May 2017

Run node file.js from php and kill it on unload - Windows

my goal is to run a node file from my php page at the start of my script and then kill that process when the page is closed. I have been reading several questions and answers without luck. I can run by example

$command = 'mybat.bat argument1 argument2 &';
exec($command, $output);

on the bat file or directly using node myfile.js, but the thing is

  1. how to let it run on the background so the rest of my php file go on?
  2. how to kill THAT process?

I am aware of the unload methods to run at the page close, but my problem is that i cant retrieve the PID from the exec, all i get on the $output is the console output from myfile.js, so i have to kill the node process manually AND as this php script may be called several times it may be hard to guess which node process to kill. I saw a function on here:

function runAsynchronously($path,$arguments) {
$WshShell = new COM("WScript.Shell");
$oShellLink = $WshShell->CreateShortcut("temp.lnk");
$oShellLink->TargetPath = $path;
$oShellLink->Arguments = $arguments;
$oShellLink->WorkingDirectory = dirname($path);
$oShellLink->WindowStyle = 1;
$oShellLink->Save();
$oExec = $WshShell->Run("temp.lnk", 7, false);
unset($WshShell,$oShellLink,$oExec);
unlink("temp.lnk");

}

but it trow me errors on my side, well i do not really understand it neither. Any lights will be REALLY appreciated. Thanks in advance.



via Aramil

No comments:

Post a Comment