0

I try to execute a .bat file (Windows Server with xampp) from php in Background. After i klick on the button, it also should go to another Website. But when i click on the button, the browser is waiting for the script to finish. Mostly this is ending in a timeout.

my php code:

if (isset($_POST['test']))
{
    exec('C:\Daten\test.bat');
    header("Location:test_status.php");
}

how can i tell the php exec, to dont wait?

I tried also following, but does not work:

exec('C:\Daten\test.bat' . '> /dev/null &');
mgat
  • 91
  • 2
  • 5
  • Have you tried `pclose(popen("start /B ". $cmd, "r"));` ?? [from here](http://php.net/manual/en/function.exec.php#86329) where `$cmd` is the command you want to run. – MoshMage Jan 13 '15 at 14:21
  • this could help http://stackoverflow.com/questions/649634/how-do-i-run-a-bat-file-in-the-background-from-another-bat-file – Ján Stibila Jan 13 '15 at 14:21
  • Related, [How do you run a .bat file from PHP?](http://stackoverflow.com/q/835941) – jww Nov 15 '16 at 03:54

3 Answers3

2

Thanks for the response, but this did not work for me. I found that many solution ideas in the internet, but nothing worked for me.

Now i finally found the solution! The keyword is popen! This works nice:

$handle = popen ('start /B C:\Data\restart_TS3.bat > C:\Daten\restart_TS3.log 2>&1', 'r');
pclose ($handle);
header("Location:ts3_status.php");

This do following: popen opens a background process that starts the batch file, output goes to the log file. Then you need to close it with pclose. (the batch is still running in background) After that, it opens another website, in my case its a "see current status" website.

If you dont want a log, you can also output (like MarkB told before) with >nul. This would look like that:

$handle = popen ('start /B C:\Data\restart_TS3.bat >nul 2>&1', 'r');

And be careful with empty spaces in the path! I dont know why, but this will not work:'start /B "C:\Data Folder\restart_TS3.bat" >nul 2>&1' In that cases you need " in the folder like that:

$handle = popen ('start /B C:\"Data Folder"\restart_TS3.bat >nul 2>&1', 'r');

This works fine with empty space in Folder names.

mgat
  • 91
  • 2
  • 5
1

You're using Windows. there's no /dev/null in Windows (it's just nul), and there's no & to run jobs in the background. & in cmd.exe is a command separator. So your exec() will hang/wait for the .bat to finish.

Try

exec('start c:\daten\test.bat');

instead, which would start the batch file as a separate process.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Good remarks about `cmd.exe` not working the same way `bash` does :-) – axiac Jan 13 '15 at 14:26
  • There is no guarantee that `cmd.exe` is invoked (on both the original solution and this answer). PHP asks the system to execute a file having the extension `.bat`, the system code consults the Windows registry and finds out that `.bat` files are passed to some command interpreter for execution. By default, it is Windows' `cmd.exe` but one can install other command interpreter and that interpreter can register itself as the handler for execution of `.bat` files. – axiac Jan 13 '15 at 14:31
1

This worked for me.

exec("start cmd /c test.bat", $output);
var_dump($output);
Rodgers Andati
  • 121
  • 1
  • 11
  • Good answer. Many people don't know that in Windows you have to include the `start` keyword at the beginning when running an `exec`. Also if you want your `exec` to be async, just add an `&` at the end. – Eduard Luca Feb 20 '15 at 12:26
  • It's always better to add an explanation of what you are trying to achieve when posting code. – Sascha Wolf Feb 20 '15 at 13:11