Execute subprocess sequentially in python
<p>I am trying to the following 2 commands in python one after another.</p>

<pre><code>runmqsc &lt;Queuem manager name&gt;
Display QL (&lt;queue name&gt;)
</code></pre>

<p>I can execute the rumqsc command using subprocess. </p>

<pre><code>subprocess.call("runmqsc &lt;queue manager name&gt;", shell= True)
</code></pre>

<p>Now this commands seems like taking the control from python. If i try to execute the next command using subprocess its not working as expected.
I am not even sure how to execute the second(for which i have to pass an argument). </p>

<p>Adding the code snippet:</p>

<pre><code>subprocess.call("runmqsc Qmgrname", shell= True)
subprocess.call("DISPLAY QL(&lt;quename&gt;)",shell=True)
</code></pre>

<p>Now the first line executes fine and as mentioned by tdelaney in the comment runmqsc waits for input from stdin. And after executing the first line the program hangs without even executing the second line.</p>

<p>Any help or references to any of the related document would help. 
Thanks</p>

<p>You don't want to run to subprocess commands sequentially. When you run <code>runmqsc</code> on the command line, it takes over <code>stdin</code>, executes the commands you enter and then finally exits when you tell it to. From <a href="http://www.ibm.com/support/knowledgecenter/SSFKSJ_9.0.0/com.ibm.mq.ref.adm.doc/q083460_.htm" rel="nofollow">the docs</a>:</p>

<blockquote>
  <p>By taking stdin from the keyboard, you can enter MQSC commands interactively.
  By redirecting the input from a file, you can run a sequence of
  frequently used commands contained in the file. You can also redirect
  the output report to a file.</p>
</blockquote>

<p>But I think there's a third way. Start <code>runmqsc</code>, write your command(s) to <code>stdin</code> then close <code>stdin</code>. It should execute the commands and exit. It turns out that <a href="https://docs.python.org/3.6/library/subprocess.html#subprocess.Popen.communicate" rel="nofollow"><code>Popen.communicate</code></a> does this for you. I don't know if you want to capture the output but in this example I'm letting it go to the screen.</p>

<pre><code># start msg queue manager
mqsc = subprocess.Popen(["runmqsc", "QMAGTRAQ01"], stdin=subprocess.PIPE,
    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# pass command(s) to manager and capture the result
out, err = mqsc.communicate("DISPLAY QL(BP.10240.012.REQUEST)")
# wait for command to complete and deal with errors
retcode = mqsc.wait()
if retcode != 0:
    print("-- ERROR --") # fancy error handling here
print("OUTPUT: ", out)
print()
print("ERROR: ", err)
</code></pre>

<p>In python3, <code>out</code> and <code>err</code> are <code>bytes</code> objects, not strings. Similar to using an encoding when you read a text file, you may have to decode them based on whatever language your program uses. Lets say the file was UTF8, then you would</p>

<pre><code>out = out.decode('utf-8')
</code></pre>

python unix subprocess websphere-mq-fte