0

I have a strange situation with a very simple batch file which I want to run from PHP, and get the exit code of this.

If I run the batch script from cmd.exe, echo %errorleve% will return the correct exit code, in this case 12. From PHP script, exit code is 0.

my_batch.bat file

@ECHO OFF

if "1"=="1" (
  if "1"=="1" (
    echo quitting
    exit /B 12
  )

  echo anything
)

my_test.php

<?php
exec('my_batch.bat',$result,$exitcode);
echo $result[0];
echo '<br />';
echo $exitcode;

Output from cmd.exe

D:\tools\xampp\htdocs\test>my_batch.bat
quitting

D:\tools\xampp\htdocs\test>echo %errorlevel%
12

D:\tools\xampp\htdocs\test>

Output oh php:

quitting
0

Thank you for your support

EDIT 1

verry strange, if I change the code with this version, all works OK

my_batch.bat file

@ECHO OFF

if "1"=="1" (
  if "1"=="1" (
    echo quitting
    SET MYERROR=12
    GOTO:END 
  )

  echo anything
)

:END
echo finished ... ERRORLEVEL "%MYERROR%"
exit /b %MYERROR%
vasilenicusor
  • 2,023
  • 1
  • 21
  • 37
  • 2
    Maybe exec the correct file? `my_batch.bat` vs `command.bat` – brombeer Sep 07 '18 at 12:04
  • sorry, the name was wrong typed. In my test all are running – vasilenicusor Sep 07 '18 at 12:07
  • The behavior I notice is that if the `exit /B 12` is outside the parentheses, then PHP gets the exitcode `12`, else it is `0`. – michael_heath Sep 07 '18 at 17:26
  • True, but the exist code works very well from CMD.exe. Why not working in this form with PHP. – vasilenicusor Sep 07 '18 at 19:47
  • As Nico Haase points, you are getting the exit code of the `cmd` process, not the one from the batch file. You can simply remove the `/B` – MC ND Sep 08 '18 at 08:41
  • @MCND yah, seems to work without /B. But verry strange, a simple batch file with only `exit /B 12` works ok. So there is a strange behaviour with the place from where `exit /B code` is called. – vasilenicusor Sep 09 '18 at 19:40
  • Jus a sample, take a look at [this](https://stackoverflow.com/a/41324747/2861476) answer to [this](https://stackoverflow.com/q/41318027/2861476) question. Error levels, conditional execution and parenthesis don't always behave as we expect. If you add to that a separate process that executes the batch file then you will find behaviours as the one observed. – MC ND Sep 10 '18 at 11:51

2 Answers2

1

There is a strange anomaly which occurs in batch file when additional commands exist in any level of parenthesis the exit /b is within.

Per your example, the exit /b 12 is the last command in parenthesis level 2 but not the last command in parenthesis level 1.

In order for the batch file to exit completely and respond with the exit code you may turn the if statement into an if/else to segregate the commands from each other like so.

if "1"=="1" (
  if "1"=="1" (
    echo quitting
    exit /b 12
  ) else (
    echo anything
  )
)
Mat Lipe
  • 725
  • 8
  • 14
0

Have you seen the documentation at http://php.net/function.exec.php? It tells you that a call of exec spawns a instance of cmd.exe and this calls your batch script. Probably, this intermediate process won't pass the return code through?

A solution could be to use proc_open

Nico Haase
  • 11,420
  • 35
  • 43
  • 69