Using subprocess module to launch a shell shell script that itselfforks a process

Discussion in 'Python' started by Samuel A. Falvo II, Oct 8, 2008.

  1. I have a shell script script.sh that launches a Java process in the
    background using the &-operator, like so:

    #!/bin/bash
    java ... arguments here ... &

    In my Python code, I want to invoke this shell script using the
    Subprocess module. Here is my code:

    def resultFromRunning_(command):
    """Invokes a shell command, and returns the stdout response.

    Args:
    command: A string containing the complete shell command to
    run.

    Results:
    A string containing the output of the command executed.

    Raises:
    ValueError if a non-zero return code is returned from the
    shell.
    OSError if command isn't found, inappropriate permissions,
    etc.
    """

    L = log4py.Logger().get_instance()
    L.info("Executing: " + command)

    p = subprocess.Popen(
    command,
    shell=True,
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    close_fds=True
    )

    outputChannel = p.stdout
    output = outputChannel.read()

    result = p.wait()
    if result:
    raise(ShellError(command, result, output))

    L.info("Result = " + str(output))
    return output

    When running the aforementioned code, it kicks off the shell script,
    and, the shell script kicks off the Java process. However, the Python
    code never returns from outputChannel.read() until I explicitly kill
    the Java process myself via the kill shell command.

    I've researched this issue on Google and various other websites, and
    maybe I'm missing the obvious, but I cannot seem to find any
    documentation relevant to this problem. Lots of references to bugs
    filed in the past, that appear to be fixed, or to websites talking
    about how the Popen module has a 64K limit on its data queue size, but
    nothing relevent to my situation.

    Can anyone inform me or point me to the appropriate documentation on
    how to properly invoke a shell command such that any spawned children
    processes don't cause Python to hang on me? I assume it has something
    to do with process groups, but I'm largely ignorant of how to control
    those.

    Thanks in advance.
    Samuel A. Falvo II, Oct 8, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Visco Shaun
    Replies:
    0
    Views:
    276
    Visco Shaun
    Feb 26, 2009
  2. Roman Medina-Heigl Hernandez

    Subprocess module: running an interactive shell

    Roman Medina-Heigl Hernandez, Mar 14, 2009, in forum: Python
    Replies:
    3
    Views:
    1,553
    Karthik Gurusamy
    Mar 15, 2009
  3. Kingsley Turner
    Replies:
    1
    Views:
    387
    Diez B. Roggisch
    Oct 14, 2010
  4. gniagnia
    Replies:
    3
    Views:
    148
    Joe Smith
    Feb 21, 2007
  5. moongeegee

    execute a shell script in a shell script

    moongeegee, Dec 3, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    250
    Ben Morrow
    Dec 4, 2007
Loading...

Share This Page