Calling a Shell Script from within Python

William Hudspeth bhudspeth at EDAC.UNM.EDU
Tue Jan 25 07:36:48 PST 2005


Hello,

I have written a Mapserver application in Python Mapscript. In my code,
I use a standard Python function to call a BASH shell script that
processes a GRASS raster file, and then loads the R statistics package
to generate some plots. When I launch the shell script from a much
simpler python module (whose only function is to call the shell script),
it runs flawlessly. In my main Mapserver application, however, I have
tried calling the shell script using two different methods in Python,
one with arguments (e.g. passing some map extents), and another without
arguments (e.g. where I hard code some map extents). They get different
errors, both seeming to involve a problem with starting R. It seems to
be a problem with running processes, but I don't understand how to
correct it:

Call #1 from my Mapserver code involves calling a locally defined
function (derived from the O'Reilly Python Standard Library Book):

        def run(program, *args):
                pid=os.fork()
                if not pid:
                        os.execvp(program, (program, ) + args)
                return os.wait()[0]
        #I then set some values for north, south, east, and west
        run(elevation_density.sh, north, south, east, west)

The error returned:

[Tue Jan 25 15:02:48 2005] [error] [client 129.24.73.222] malformed
header from script. Bad header=Setting GISRC environmental sh:
python_mapscript.py, referer: http://falstaff.unm.edu/cgi-
bin/reason/python_mapscript.py
[Tue Jan 25 15:02:48 2005] [error] [client 129.24.73.222] /usr/local/grass53/scripts/elevation_density.sh: line 75: R: command not found, referer: http://falstaff.unm.edu/cgi-bin/reason/python_mapscript.py

Call#2 from my Mapserver code uses the os.system() function (where I
hard code some extents in the Bash shell script)

        os.system("elevation_density.sh")

The error returned is basically the same as above.

Again, a simple python module with one of the two function calls above
works just fine. It is only when I make the calls from within my larger
block of Mapserver Python code that I get errors.

Any help would be greatly appreciated.

Bill



More information about the MapServer-users mailing list