Python subprocess not returning
<p>I want to call a Python script from Jenkins and have it build my app, FTP it to the target, and run it.</p>

<p>I am trying to build and the <code>subprocess</code> command fails. I have tried this with both <code>subprocess.call()</code> and <code>subprocess.popen()</code>, with the same result.</p>

<p>When I evaluate <code>shellCommand</code> and run it from the command line, the build succeeds.</p>

<p>Note that I have 3 shell commands: 1) remove work directory, 2) create a fresh, empty, work directory, then 3) build. The first two commands return from <code>subprocess</code>, but the third hangs (although it completes when run from the command line).</p>

<p>What am I doing wrongly? Or, what alternatives do I have for executing that command?</p>

<pre><code># +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
def ExcecuteShellCommandAndGetReturnCode(arguments, shellCommand):
    try:
        process = subprocess.call(shellCommand, shell=True, stdout=subprocess.PIPE)
        #process.wait()
        return process #.returncode

    except KeyboardInterrupt, e: # Ctrl-C
        raise e
    except SystemExit, e: # sys.exit()
        raise e
    except Exception, e:
        print 'Exception while executing shell command : ' + shellCommand
        print str(e)
        traceback.print_exc()
        os._exit(1)

# +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
def BuildApplciation(arguments):
    # See http://gnuarmeclipse.github.io/advanced/headless-builds/

    jenkinsWorkspaceDirectory = arguments.eclipseworkspace + '/jenkins'

    shellCommand = 'rm -r ' + jenkinsWorkspaceDirectory
    ExcecuteShellCommandAndGetReturnCode(arguments, shellCommand)

    shellCommand = 'mkdir ' + jenkinsWorkspaceDirectory
    if not ExcecuteShellCommandAndGetReturnCode(arguments, shellCommand) == 0:
        print "Error making Jenkins work directory in Eclipse workspace : " + jenkinsWorkspaceDirectory
        return False

    application = 'org.eclipse.cdt.managedbuilder.core.headlessbuild'
    shellCommand = 'eclipse -nosplash -application ' + application + ' -import ' + arguments.buildRoot + '/../Project/ -build myAppApp/TargetRelease -cleanBuild    myAppApp/TargetRelease -data ' + jenkinsWorkspaceDirectory + ' -D DO_APPTEST'
    if not ExcecuteShellCommandAndGetReturnCode(arguments, shellCommand) == 0:
        print "Error in build"
        return False
</code></pre>

<p>Ok, I go it. No comments or answers so far, but, rather then delete the question, I will leave it here in case it helps anyone in future.</p>

<p>I Googled further and found <a href="https://www.webcodegeeks.com/python/python-subprocess-example/" rel="nofollow">this page</a>, which, at 1.2 says  </p>

<blockquote>
  <p>One way of gaining access to the output of the executed command would
  be to use PIPE in the arguments stdout or stderr, but the child
  process will block if it generates enough output to a pipe to fill up
  the OS pipe buffer as the pipes are not being read from.</p>
</blockquote>

<p>Sure enough, when I deleted the <code>, stdout=subprocess.PIPE</code> from the code above, it worked as expected.</p>

<p>As I only want the exit code from the subprocess, the above code is enough for me. Read the linked page if you want the output of the command.</p>

python python-2.7 subprocess