subprocess returncode windows

Andrew andrew.replogle at gmail.com
Thu Feb 5 14:34:29 EST 2009


On Dec 16 2008, 5:11 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Tue, 16 Dec 2008 17:21:35 -0200, Andrew <andrew.replo... at gmail.com>  
> escribió:
>
>
>
> > On Dec 16, 12:50 pm, Christian Heimes <li... at cheimes.de> wrote:
> >> Andrew schrieb:
>
> >> > I'm running into a strange situation with getting incorrect
> >> > returncodes / exit status from python subprocess.call. 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 removed shell=True, unfortunately, if I structure the call like:
>
> > call(["c:/glassfish/bin/asadmin.bat", "list-system-properties", "--
> > host
> > mydomain", "--port 4848", "--user admin", "server-01"])
>
> > It doesn't seem to recognize any arguments after list-system-
> > properties.
>
> Should be:
>
> call(["c:/glassfish/bin/asadmin.bat", "list-system-properties", "--host",
> "mydomain", "--port", "4848", "--user", "admin", "server-01"])
>
> *Every* argument should be an item in the list (your way, "--port 4848"  
> becomes a single argument, not two: option plus value)
> (This is independent of your other issue)
>
> > If I structure it like:
>
> > call("c:/glassfish/bin/asadmin.bat "+"list-system-properties --host
> > mydomain --port 4848 --user admin server-01")
>
> > Then it executes correctly but still gives invalid returncode of 0
> > when it fails instead of 1.
>
> A similar example works fine for me:
>
> C:\temp>type ret.c
> #include <stdlib.h>
>
> int main(int argc, char* argv[])
> {
>    return atoi(argv[1]);
>
> }
>
> C:\temp>ret 5
>
> C:\temp>echo %errorlevel%
> 5
>
> C:\temp>type testret.bat
> ret %1
>
> C:\temp>testret 3
>
> C:\temp>ret 3
>
> C:\temp>echo %errorlevel%
> 3
>
> C:\temp>type testret.py
>  from subprocess import call
> ret = call(["testret.bat", "42"])
> print "testret.bat exit code =", ret
>
> C:\temp>python testret.py
>
> C:\temp>ret 42
> testret.bat exit code = 42
>
> C:\temp>python -V
> Python 2.6
>
> C:\temp>ver
>
> Microsoft Windows XP [Versión 5.1.2600]
>
> --
> Gabriel Genellina

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:

------
C:\temp>c:\glassfish\bin\asadmin.bat list-system-properties
Instance-01
....
properties here
....

Command list-system-properties executed successfully.

C:\temp>echo %errorlevel%
0

C:\temp>c:\glassfish\bin\asadmin.bat list-system-properties
Instance-05    //note that Instance-05 does not exist
Cannot determine type for target : Instance-05
CLI137 Command list-system-properties failed.

C:\temp>echo %errorlevel%
1

C:\temp>ping 019293.com
Ping request could not find host 019293.com. Please check the name and
try again
.

C:\temp>echo %errorlevel%
1

C:\temp>ping google.com

Pinging google.com [74.125.45.100] with 32 bytes of data:

Reply from 74.125.45.100: bytes=32 time=48ms TTL=234
Reply from 74.125.45.100: bytes=32 time=66ms TTL=234
Reply from 74.125.45.100: bytes=32 time=63ms TTL=234
Reply from 74.125.45.100: bytes=32 time=44ms TTL=234

Ping statistics for 74.125.45.100:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 44ms, Maximum = 66ms, Average = 55ms

C:\temp>echo %errorlevel%
0
----------------------------------------

Then I tried the following in python (2.6.1)

---script---
import subprocess
import sys


try:
    retcode = subprocess.call(["ping","019293.com"])
    if retcode < 0:
        print >>sys.stderr, "Child was terminated by signal", -retcode
    else:
        print >>sys.stderr, "Child returned", retcode
except OSError, e:
    print >>sys.stderr, "Execution failed:", e

try:
    retcode = subprocess.call(["ping","google.com"])
    if retcode < 0:
        print >>sys.stderr, "Child was terminated by signal", -retcode
    else:
        print >>sys.stderr, "Child returned", retcode
except OSError, e:
    print >>sys.stderr, "Execution failed:", e


try:
    retcode = subprocess.call(["c:/glassfish/bin/asadmin.bat","list-
system-properties","Instance-01"], shell=False)
    if retcode < 0:
        print >>sys.stderr, "Child was terminated by signal", -retcode
    else:
        print >>sys.stderr, "Child returned", retcode
except OSError, e:
    print >>sys.stderr, "Execution failed:", e

try:
    retcode = subprocess.call(["c:/glassfish/bin/asadmin.bat","list-
system-properties","Instance-05"], shell=False)
    if retcode < 0:
        print >>sys.stderr, "Child was terminated by signal", -retcode
    else:
        print >>sys.stderr, "Child returned", retcode
except OSError, e:
    print >>sys.stderr, "Execution failed:", e
---script---

Executed Output:

---output---
C:\temp>c:\Python26\python.exe example2.py
Ping request could not find host 019293.com. Please check the name and
try again.
Child returned 1

Pinging google.com [74.125.67.100] with 32 bytes of data:

Reply from 74.125.67.100: bytes=32 time=244ms TTL=239
Reply from 74.125.67.100: bytes=32 time=244ms TTL=239
Reply from 74.125.67.100: bytes=32 time=191ms TTL=234
Reply from 74.125.67.100: bytes=32 time=59ms TTL=239

Ping statistics for 74.125.67.100:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 59ms, Maximum = 244ms, Average = 184ms
Child returned 0

...glassfish properties for Instance-01 display here...
Command list-system-properties executed successfully.
Child returned 0

Cannot determine type for target : Instance-05
CLI137 Command list-system-properties failed.
Child returned 0

C:\temp>
---output---

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?

TIA,

Andrew




More information about the Python-list mailing list