import subprocess import traceback import argparse import signal if __name__ == '__main__': parser = argparse.ArgumentParser( prog='zaebaka', description='bash cat with a pipe', ) parser.add_argument( '-s', '--script', type=str, required=True, help='script to run in shell', ) parser.add_argument( '-c', '--count', type=int, default=1, help='instances count', ) parser.add_argument( '-t', '--template', type=str, default='{{instance}}', help='template placeholder for instance id substitution', ) args = parser.parse_args() processes = [] def signal_handler(sig, frame): print('Terminaring subprocesses') for process in processes: try: process.terminate() except: traceback.print_exc() signal.signal(signal.SIGINT, signal_handler) for instance in range(args.count): script = args.script.replace(args.template, str(instance)) print(f'cmd: `{ script }`') processes.append( subprocess.Popen( script, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) ) print('Press Ctrl+C to exit') signal.pause()