Interrupt script without ending bat

0

A PHP script generates a .bat file and then executes it, the problem is that I do not want my PHP script to wait until .bat finishes running. I just want it to run, create and run the bat and then shut down without waiting for the bat to finish, how can I do that? I've tried adding > /dev/null 2>/dev/null & to exec but it did not work.

    
asked by anonymous 09.04.2017 / 02:06

1 answer

3

If you're on Windows, try using:

$WshShell = new COM("WScript.Shell");
$WshShell->Run('%COMSPEC% /C '.$file, 0, false);

If you do not want to trust at %COMSPEC% specify which " interpreter " you want, usually %WINDIR%\system32\cmd.exe is the default and it will be used.

Test this:

  

my.bat

@ECHO off
TIMEOUT /T 5 /NOBREAK
START %WINDIR%\system32\calc.exe

This will wait 5 seconds and then open the calculator ( %WINDIR%\system32\calc.exe ).

  

my.php

<?php

$inicio = time();
$arquivo = getcwd().DIRECTORY_SEPARATOR.'meu.bat';

$WshShell = new COM("WScript.Shell");
$WshShell->Run('%COMSPEC% /C '.$arquivo, 0, false);

$fim = time();

echo 'Finalizado em '.($fim - $inicio).' segundos';

Outcome:

PHP will return the text "Finished in 0 seconds", meanwhile the CMD will open the calculator after 5 seconds.

The PHP return is before the end of the CMD run. ;)

Another observation is that if PHP is running "on the same client machine" ( as in localhost ), no CMD window will be shown to the user, thus allowing to execute any command silently .

You need to enable the php_com_dotnet.dll extension in PHP.ini!

    
09.04.2017 / 22:34