Abort script without closing bat

Asked

Viewed 546 times

0

A PHP script generates a file .bat and then runs the same, the problem is that I do not want my PHP script to be expecting such .bat finish running. I just want it to run, create and run the bat and then close without waiting for the bat to finish, how can I do that? I’ve tried adding > /dev/null 2>/dev/null & at the exec but it didn’t work.

  • Create another bat that has the function of just starting the slow bat. Example: start myBatch.bat.

  • @Laérciolopes I created here a bat to start the main bat, and yet PHP stays locked until the bat is finished running.

1 answer

3


If you’re on Windows, try using:

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

If you don’t want to entrust in the %COMSPEC% specify which "interpreter" wishes, usually the %WINDIR%\system32\cmd.exe is the standard and it will be used.


Test this:

my bat.

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

This will make you 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';

Upshot:

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

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

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.

inserir a descrição da imagem aqui


It is necessary to enable the extension php_com_dotnet.dll in PHP.ini!

  • after running the bat it is possible to run a script warning that the process is over?

  • Once the page is sent no. What you can do is call a PHP, for example start c:\caminho\para\cli\php.exe outro.php and that file is responsible for notifying the user (via websockets) or updating something in the database (or text file) and for ajax-Polling finding that it is over. Another option would be to go, in each ajax-Polling, seeing the tasklist, thus seeing whether the process has ended or not. At least, it is the only solution I see.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.