title: Python and subprocess input piping
body: <p>I have a small script that launches and, every half hour, feeds a command to a java program (game server manager) as if the user was typing it.  However, after reading documentation and experimenting, I can't figure out how I can get two things:</p>
  
  <p>1) A version which allows the user to type commands into the terminal windoe and they will be sent to the server manager input just as the "save-all" command is.</p>
  
  <p>2) A version which remains running, but sends any new input to the system itself, removing the need for a second terminal window.  This one is actually half-happening right now as when something is typed, there is no visual feedback, but once the program is ended, it's clear  the terminal has received the input.  For example, a list of directory contents will be there if "dir" was typed while the program was running.  This one is more for understanding than practicality.</p>
  
  <p>Thanks for the help.  Here's the script:</p>
  
  <pre><code>from time import sleep
  import sys,os
  import subprocess
  
  
  #  Launches the server with specified parameters, waits however
  #  long is specified in saveInterval, then saves the map.
  
  
  #  Edit the value after "saveInterval =" to desired number of minutes.
  #  Default is 30
  
  saveInterval = 30
  
  #  Start the server.  Substitute the launch command with whatever you please.
  p = subprocess.Popen('java -Xmx1024M -Xms1024M -jar minecraft_server.jar',
                       shell=False,
                       stdin=subprocess.PIPE);
  
  while(True):
  
      sleep(saveInterval*60)
  
      #  Comment out these two lines if you want the save to happen silently.
      p.stdin.write("say Backing up map...\n")
      p.stdin.flush()
  
      #  Stop all other saves to prevent corruption.
      p.stdin.write("save-off\n")
      p.stdin.flush()
      sleep(1)
  
      #  Perform save
      p.stdin.write("save-all\n")
      p.stdin.flush()
      sleep(10)
  
      #  Allow other saves again.
      p.stdin.write("save-on\n")
      p.stdin.flush()
  </code></pre>