Search code examples
phpwindowsbackgroundkillnohup

PHP on a windows machine; Start process in background


I'm looking for the best, or any way really to start a process from php in the background so I can kill it later in the script.

Right now, I'm using: shell_exec($Command); The problem with this is it waits for the program to close.

I want something that will have the same effect as nohup when I execute the shell command. This will allow me to run the process in the background, so that later in the script it can be closed. I need to close it because this script will run on a regular basis and the program can't be open when this runs.

I've thought of generating a .bat file to run the command in the background, but even then, how do I kill the process later?

The code I've seen for linux is:

$PID = shell_exec("nohup $Command > /dev/null & echo $!");
// Later on to kill it
exec("kill -KILL $PID");

EDIT: Turns out I don't need to kill the process


Solution

  • Will this function from the PHP Manual help?

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