subprocess returncode windows

Andrew andrew.replogle at gmail.com
Tue Dec 16 20:21:35 CET 2008


On Dec 16, 12:50 pm, Christian Heimes <li... at cheimes.de> wrote:
> Andrew schrieb:
>
>
>
> > Hello,
>
> > 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
> > of using a subprocess call to test the success / failure of the
> > glassfish CLI tool "asadmin"
>
> > Example.py:
> > ----------------------
> > import sys
> > from subprocess import *
>
> > try:
> >     retcode = call("c:/glassfish/bin/asadmin.bat " + "list-system-
> > properties --host mydomain --port 4848 --user admin server-01",
> > shell=True)
> >     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
>
> Don't use shell=True! Instead use a list of arguments without shell=True:
>
> call(["c:/glassfish/bin/asadmin.bat", "list-system-properties", "--host
> mydomain", "--port 4848", "--user admin", "server-01"])
>
> That should solve your quoting issues on Windows and fix your code.
> shell=True is considered evil and should be avoided whenever possible.
>
> Christian

Thanks Christian,

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. 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.

Andrew




More information about the Python-list mailing list