title: Start daemon process within BaseHTTPServer
body: <p>In order to allow helpdesk to restart an Oracle Instance, we are trying to implement a small python webserver that would start a shell script that starts the Oracle instance.</p>
  
  <p>The code is done and it starts the instance but there is a problem: the instance is connected to the webserver, so the buffer to the browser is not closed until the instance has been stopped, and there is a <code>ora_pmon_INSTANCE</code> process listening on the webserver port.</p>
  
  <p>I tried to launch the script with:</p>
  
  <pre><code>process = os.system("/home/oracle/scripts/webservice/prueba.sh TFINAN")
  </code></pre>
  
  <p>and</p>
  
  <pre><code>process = subprocess.Popen(["/home/oracle/scripts/webservice/prueba.sh", "TFINAN"], shell=False, stdout=subprocess.PIPE)`
  </code></pre>
  
  <p>but it happens the same.</p>
  
  <p>I also tried to launch a script with daemon (using daemon function from redhat's init scripts). The script starts the Oracle instance with the same result.</p>
  
  <p>This is my code:</p>
  
  <pre><code>#!/usr/bin/python
  
  from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  from SocketServer import ThreadingMixIn
  import threading
  import argparse, urlparse
  import re
  import cgi, sys, time
  import os, subprocess
  
  class HTTPRequestHandler(BaseHTTPRequestHandler):
  
      def do_POST(self):
          self.send_response(403)
          self.send_header('Content-Type', 'text/html')
          self.end_headers()
  
          return
  
      def do_GET(self):
          ko = False
          respuesta = ""
          params = {}
          myProc = -1
          parsed_path = urlparse.urlparse(self.path)
          try:
              params = dict([p.split('=') for p in parsed_path[4].split('&amp;')])
          except:
              params = {}
  
          elif None != re.search('/prueba/*', self.path):
              self.send_response(200)
              respuesta = "Hola Mundo! --&gt;" + str( params['database'] )
  
          elif None != re.search('/startup/*', self.path):
              self.send_response(200)
              self.send_header('Content-Type', 'text/html')
              self.end_headers()
              cmd = """ &lt;html&gt;
                          &lt;body&gt;&lt;H2&gt; Iniciando instancia oracle: """ + str( params["database"]) + '. Espere un momento, por favor ...&lt;/H2&gt;'
  
              self.wfile.write(cmd)
  
              #process = os.system("/home/oracle/scripts/webservice/prueba.sh INSTANCE")
              process = subprocess.Popen(["/home/oracle/scripts/webservice/prueba.sh", "INSTANCE"], shell=False, stdout=subprocess.PIPE)
              # wait for the process to terminate
              out, err = process.communicate()
              errcode = process.returncode
              if errcode == 0:
                  self.wfile.write("""&lt;H1&gt; Instancia iniciada correctamente
                                  &lt;/H1&gt;
                              &lt;/body&gt; &lt;/html&gt;""")
                  self.wfile.close()
              else:
                  respuestaok = "Error inicializando la instancia: " + str( params['database']) + " Intentelo de nuevo pasados unos minutos y si vuelve a fallar escale la incidencia al siguiente nivel de soporte"
  
          else:
              self.send_response(403, 'Bad Request: pagina no existe')
              respuesta = "Solicitud no autorizada"
  
          if respuesta != "":
              self.send_response(200)
              self.send_header('Content-Type', 'text/html')
              self.end_headers()
              self.wfile.write(respuesta)
              self.wfile.close()
  
          if ko:
              server.stop()           
  
          return
  
  
  class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
      allow_reuse_address = True
  
      def shutdown(self):
          self.socket.close()
          sys.exit(0)
  
  class SimpleHttpServer(object):
      def __init__(self, ip, port):
          self.server = ThreadedHTTPServer((ip,port), HTTPRequestHandler)
  
      def start(self):
          self.server_thread = threading.Thread(target=self.server.serve_forever)
          self.server_thread.daemon = True
          self.server_thread.start()
  
      def waitForThread(self):
          self.server_thread.join()
  
      def stop(self):
          self.server.shutdown()
  
  if __name__=='__main__':
      parser = argparse.ArgumentParser(description='HTTP Server')
      parser.add_argument('port', type=int, help='Listening port for HTTP Server')
      parser.add_argument('ip', help='HTTP Server IP')
      args = parser.parse_args()
  
      server = SimpleHttpServer(args.ip, args.port)
      print 'HTTP Server Running...........'
      server.start()
      server.waitForThread()
  </code></pre>
  
  <p>Can any of you help me?</p>