title: Python program doesn`t run in the background even after using & .. why?
body: <p>I have a python program which curls to a machine and tries to run that file and if the file runs in 300secs its fine else it kills it.</p>
  
  <pre><code>import threading,datetime,signal,os
  from threading import Thread
  import logging,time
  from subprocess import Popen,PIPE
  import subprocess,os,errno
  from xmlrpclib import ServerProxy
  
  
  
  logging.basicConfig(level=logging.INFO)
  logger = logging.getLogger(__name__)
  
  id = 1
  p=Popen('hostname',stdout=PIPE,stderr=PIPE)
  out,err=p.communicate()
  global hostname
  hostname=out.replace('\n','')
  dir_path='/export/home/host/'+str(hostname)
  curl_path='http://1.1.1.1:8080/host_exec/'+str(hostname)
  
  
  
  def run_file(new_id,data):
  
      data=data.split(',')
      if len(data)&gt;1:
          session=Popen(str(data[0])+' '+str(data[1]),shell=True,stdout=PIPE,stderr=PIPE,stdin=PIPE)
      else:
          session=Popen(str(data[0]),shell=True,stdout=PIPE,stderr=PIPE,stdin=PIPE)
  
      start = datetime.datetime.now()
      print 'pid is ',session.pid
  
      stdout1=''
      stderr1=''
  
      while session.poll() is None:
          print 'sleeping'
          time.sleep(10)
          now = datetime.datetime.now()
          #Kill the process if its still running and timeout period is over ( max 20 secs )
          if (now - start).seconds &gt; 10:
              os.kill(session.pid, signal.SIGKILL)
              os.waitpid(-1, os.WNOHANG)
              killed_flag=1
              break;
          #This condition is checked to see if the process finished executing and doesn`t need to be killed - success
          elif session.poll() is not None:
              print 'Process has finished'
              stdout1=session.stdout.read()
              stderr1=session.stdout.read()
              break;
          #If timeout is still remaining - wait for it to finish or get killed
          else:
              print 'still executing'
  
  
      if killed_flag==1:
          stdout1=' THE SCRIPT COULDN`T COMPLETE EXECUTION ON HOSTNAME '+str(hostname)+' AS IT TOOK MORE THAN 5 MINUTES TO FINISH AND HENCE GOT KILLED : TRY RERUNNING IT OR RUN IT MANUALLY ON THE BOX'
          print stdout1
  
  
  
      ops=stdout1 + stderr1
      s = ServerProxy('http://paste/',allow_none=True)
      page_url=s.pastes.newPaste("text",ops,None)
      #print page_url
      #print stdout
  
      #Connect to db and update the table with the clob generated
      session = Popen(['sqlplus','-S','abc/abc:1500'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
      if flag==1:
         #print flag
         sql='update abc set temp_op = temp_op || \''+str(page_url)+',\', temp_db = temp_db || \''+str(hostname)+',\' where job_id=\''+str(new_id)+'\' ;'
  
  
      if flag==2:
          #print flag
          sql='update abc config set output=\''+str(page_url)+'\', job_status=\'Completed\' where job_id=\''+str(new_id)+'\' ;'
  
      session.stdin.write(sql)
      session.stdin.flush()
      stdout, stderr = session.communicate()
      #print stdout
      #print stderr
  
  
  
  def do_this():
      print "Running with Current Id : ", id
      session=Popen('/export/home/curl '+str(curl_path)+'/filenames.txt',shell=True,stdout=PIPE,stderr=PIPE,stdin=PIPE)
      stdout,stderr= session.communicate()
  
      files_in_dir=stdout.split(' ')
      if(len(files_in_dir)&gt;1):
          print files_in_dir
          for file_name in files_in_dir:
              if file_name:
                  file_list=file_name.split('_')
                  new_id=file_list[1]
  
                  if new_id&gt;id:
                      session=Popen('/export/home/curl '+str(curl_path)+'/'+str(file_name),shell=True,stdout=PIPE,stderr=PIPE,stdin=PIPE)
                      file_content,stderr= session.communicate()
                      t = Thread(target=run_file,args=(new_id,file_content,))
                      t.start()
                      global id
                      id = new_id
                  else:
                      print 'No new file to process'
      else:
          print "EMPTY FOLDER"
  
  
  
  while True:
      do_this()
      time.sleep(10)
  </code></pre>
  
  <p>But when I run it like</p>
  
  <pre><code>python abc.py &amp;   
  </code></pre>
  
  <p>It doesn`t run in the background. Why?</p>
  
  <p>Also, When I do a CTRL+C or COMMAND+C to kill it .. it still keeps on running.</p>