execute a shell-script from Python subprocess

27,101

Using the subprocess library, you can tell the Popen class that you want to manage the standard input of the process like this:

import subprocess
shellscript = subprocess.Popen(["shellscript.sh"], stdin=subprocess.PIPE)

Now shellscript.stdin is a file-like object on which you can call write:

shellscript.stdin.write("yes\n")
shellscript.stdin.close()
returncode = shellscript.wait()   # blocks until shellscript is done

You can also get standard out and standard error from a process by setting stdout=subprocess.PIPE and stderr=subprocess.PIPE, but you shouldn't use PIPEs for both standard input and standard output, because deadlock could result. (See the documentation.) If you need to pipe in and pipe out, use the communicate method instead of the file-like objects:

shellscript = subprocess.Popen(["shellscript.sh"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = shellscript.communicate("yes\n")   # blocks until shellscript is done
returncode = shellscript.returncode
Share:
27,101
theAlse
Author by

theAlse

Embedded Software Engineer SOreadytohelp

Updated on June 01, 2020

Comments

  • theAlse
    theAlse almost 4 years

    I need to call a shellscript from python. The problem is that the shellscript will ask a couple of questions along the way until it is finished.

    I can't find a way to do so using subprocess! (using pexpect seems a bit over-kill since I only need to start it and send a couple of YES to it)

    PLEASE don't suggest ways that requires modification to the shell-script!

  • jfs
    jfs almost 11 years
    stdout, stderr should be set in the second Popen call otherwise .communicate() returns Nones (no redirection)
  • Jim Pivarski
    Jim Pivarski almost 11 years
    Ah, right--- that's what I meant, but I'll edit the answer to be more explicit. (Or, maybe you already did? Anyway, looks right to me now.)
  • jfs
    jfs almost 11 years
    also, you could from subprocess import Popen, PIPE to avoid subprocess. prefix for readability