title: Running Google Cloud SQL Proxy on background for python
body: <p>I'm running Google Cloud SQL Proxy on a Raspberry with Ubuntu mate, but currently, I have to open one terminal to start the proxy and another to run my python SQL script. I need to automate this process.</p>
  
  <p>To start the proxy I run:</p>
  
  <pre><code>cd my_proxy_dir
  sudo ./cloud_sql_proxy -dir=./ -instance_name -credential_file=./key_file.json
  </code></pre>
  
  <p>The last line of the output is</p>
  
  <blockquote>
    <p>Ready for connections</p>
    
    <p>█ (the square is blinking, the terminal is blocked, the proxy owns the terminal)</p>
  </blockquote>
  
  <p>In a second terminal I run my python script which includes a MySQL connection to the cloud instance, and the output of the first terminal reports the new connection, at the end of my python script the connection is also notified:</p>
  
  <blockquote>
    <p>(timestamp) Ready for connections ...</p>
    
    <p>(timestamp) New connection ... </p>
    
    <p>(timestamp) Connection finished for ...</p>
    
    <p>█ (the square is blinking, the terminal is blocked, the proxy owns the terminal)</p>
  </blockquote>
  
  <p>Now, since I'm working with python, I created a thread because I was unable to run the proxy starting command without blocking. This is my run method:</p>
  
  <pre><code>my_thread...    
      def run(self)
              subprocess.call(['sudo', './proxy/cloud_sql_proxy', '-dir=./proxy/', '-instances=instance_name','-credential_file=./proxy/key_file.json', '&amp;'])
             print('pass')
  </code></pre>
  
  <p>It never print "pass" until I terminate the thread with thread.terminate()     </p>
  
  <p>Then what I do</p>
  
  <pre><code>my_proxy_thread = Thread_proxy()
  my_proxy_thread.start()
  time.sleep(10) #because I don't know when the proxy socket has bee created so I only wait
  # Once created the socket
  do_my_mysql_stuff()
  my_proxy_thread.terminate() # kill the thread and also the proxy socket in the bad way
  </code></pre>
  
  <p>And it works, my proxy socket is created, my_sql works ok but I have no control of the proxy output, I don't know what is happening, if it failed or is closed, working... I cannot send the instruction to close the socket. And I get a mess in my terminal where I do logging because of the subprocess command output</p>
  
  <p>What do I need?</p>
  
  <ol>
  <li>It would be great if I could get the stdout of the sub-process, so I could realize when the socket has been created and connected, but until now I haven't been able, even with PIPE it blocks.</li>
  <li>I don't want that the result of the subprocess be printed in the terminal, because I'm using the logging package, so the stdout of the subprocess creates disorder in my terminal logs :(</li>
  <li>Send a Control + C to the subprocess to stop the proxy instead of terminating the read so roughly</li>
  </ol>
  
  <p>Thanks to all :)</p>