Issue
I have a Python script that needs to issue a number of shell commands. I thought I could just create a subprocess object and then reuse it each time I had a command to execute.
This is how I have my code set up:
def setupPipeline(self):
setupShell = subprocess.Popen([''], stdout=subprocess.PIPE, shell=True, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
# Stop running pipeline
setupShell.stdin.write('stop ' + self.Name)
output = setupShell.stdout.read()
print output
# Cancel any running jobs and cleanup variables
setupShell.stdin.write('sudo -u pr cancel ALL')
output = setupShell.stdout.read()
print output
setupShell.stdin.write('sudo -u pr clean ALL')
output = setupShell.stdout.read()
print output
(skip a lot of other code here)
if __name__ == '__main__':
#self-test code
pipelineObj = Pipeline(sys.argv)
pipelineObj.setupPipeline()
However, when the code gets to the second command I get a
IOError: [Errno 32] Broken pipe
How do I go about reusing a subprocess object to issue commands that need to all execute within the same shell? These commands can't be simply chained together since there is processing going on in between each call.
Solution
Create a subprocess that executes a shell, and send commands to that. You're running an empty command in the shell, which causes it to execute that empty command and then exit.
shell = subprocess.Popen("/bin/bash -i".split(), stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
You might instead consider using pexpect
as it sounds like you are well on your way to reinventing it.
Answered By - kindall Answer Checked By - Candace Johnson (WPSolving Volunteer)