subprocess returncode windows

Gabriel Genellina gagsl-py2 at
Fri Feb 6 04:17:01 CET 2009

En Thu, 05 Feb 2009 17:34:29 -0200, Andrew <andrew.replogle at>  

> On Dec 16 2008, 5:11 pm, "Gabriel Genellina" <gagsl-... at>
> wrote:
>> En Tue, 16 Dec 2008 17:21:35 -0200, Andrew <andrew.replo... at>  
>> escribió:
>> > On Dec 16, 12:50 pm, Christian Heimes <li... at> wrote:
>> >> Andrew schrieb:
>> >> > I'm running into a strange situation with getting incorrect
>> >> > returncodes / exit status from python I'm using a
>> >> > python script (runtime 2.6.1 on windows) to automate the deploy of
>> >> > java applications to glassfish application server. Below is an >>  
>> > example

> I've tried this several ways now. It seems to be something specific
> with python and asadmin.bat.
> I've tried the following manually in the cmd.exe prompt:

[examples showing %ERRORLEVEL% correctly set when running from the command  
line, but doesn't get it]

> Notice how python never gets the correct returncode from asadmin.bat
> but I can get the correct returncode from the shell every time. Can
> anyone tell me why Python wouldn't be able to get the correct
> returncode for asadmin?

The last exit code set by a command *should* propagate as the exit code of  
the whole .bat, then as the exit code of the cmd.exe instance that runs  
it, and finally Python *should* receive that value. Some old Windows  
versions didn't behave like that, but AFAIK XP does the right thing here.
Unless asadmin.bat is playing tricks with %ERRORLEVEL% or something.
Can you post the contents of asadmin.bat?

Without looking into it, I can think of a few alternatives:

- rewrite asadmin.bat in Python, if feasible. Some scripts just check/set  
a few environment variables and execute some process at the end, and  
that's all; in this case it should be easy to emulate the same thing in  

- try using another layer of your own, e.g., my_asadmin.bat:

call asadmin.bat %*
exit /b %ERRORLEVEL%

- variation: write the exit code somewhere:

call asadmin.bat %*
echo %ERRORLEVEL% > asadmin.err

and read asadmin.err from Python. (I've used something like this in a  
chain Win32 process --> 16 bits GUI application --> .bat script --> old  
DOS executable)

Gabriel Genellina

More information about the Python-list mailing list