Advertisement
shavenwarthog

run multiple commands without subprocess/multiprocessing

Jun 27th, 2014
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. import os, select, signal, subprocess, sys
  2.  
  3. CMD = 'tail -f /var/log/syslog'
  4.  
  5. def signal_cleanup(_signum, _frame):
  6.     print '\nCLEANUP\n'
  7.     sys.exit(0)
  8.  
  9. def main():
  10.     hosts = ['localhost', 'localhost']
  11.  
  12.     # exit after a few seconds
  13.     signal.signal(signal.SIGALRM, signal_cleanup)
  14.     signal.alarm(10)
  15.  
  16.     procs = [
  17.         subprocess.Popen(
  18.             'ssh {} {}'.format(host, CMD),
  19.             shell=True,
  20.             stdout=subprocess.PIPE,
  21.             )
  22.         for host in hosts
  23.     ]
  24.  
  25.     while True:
  26.         inputs,_outputs,_excepts = select.select(
  27.             [p.stdout for p in procs], [], [],
  28.         )
  29.         for fd in inputs:
  30.             print fd.readline(),
  31.  
  32.     print 'done!'
  33.  
  34. if __name__=='__main__':
  35.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement