title: Python: how to invoke tmux in background
body: <p>I was trying to invoke tmux and to control the tmux from the same python script. It's stuck in tmux process creation and never returns. How do I create the process (tmux)?</p>
  
  <pre><code>#!/usr/bin/python
  import time
  import sys
  import os
  import subprocess
  
  class Tmux(object):
  
      WAIT_SECS = 5
  
      def __init__(self, session_name, param):
          self.name = session_name
  #       os.system('tmux new -s %s &amp;' % session_name)
          subprocess.Popen(['/bin/bash', '-c', 'tmux new -s %s' % session_name])
  #       subprocess.check_call('tmux new -s %s' % session_name, shell=True, close_fds=True)
  # ======NOT return here, unless exit from tmux==========
  
      def execute(self, cmd, wait_secs=WAIT_SECS):
          time.sleep(wait_secs)
          os.system('tmux send-keys -t %s ls Enter' % self.name)
  
      def horizontal_split(self):
          time.sleep(1)
          os.system('tmux split-window -v -t %s' % self.name)
  
  if __name__ == '__main__':
      print 'tmux test............'
      tmux = Tmux("tmux-test", 999)
      tmux.horizontal_split()
      tmux.execute('ls')
  </code></pre>