Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8138543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:31:20+00:00 2026-06-06T11:31:20+00:00

I have a Python script that needs to issue a number of shell commands.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:31:23+00:00Added an answer on June 6, 2026 at 11:31 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python script that needs to process a large number of files.
I have a script in python that needs to read iso-8859-1 files and also
I have a Python script that uses built-in modules but also imports a number
I have a Python script that is running a few ls commands. This script
Let's pretend (since it's true) that I have a Python (3) script that needs
I have a python script that needs to call the defined $EDITOR or $VISUAL
I have a Python script that needs to execute an external program, but for
I'm working on a Python script that needs to create about 50 distinct temporary
I have a python script that uses threads and makes lots of HTTP requests.
I have a python script that uses plt.show() as it's last instruction. When it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.