From 019ad02c6a6a49e0a87e664b39b00b4fb8ba385c Mon Sep 17 00:00:00 2001 From: pegasko Date: Wed, 27 Nov 2024 20:06:59 +0300 Subject: [PATCH] zaebaka --- README.md | 23 +++++++++++++++++++ zaebaka.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 README.md create mode 100644 zaebaka.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..5be1496 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# zaebaka + +bash cat with a pipe + +... or in another words, spawn N processes running someting and kill them when done. + +# Usage + +``` +$ python zaebaka.py -h +usage: zaebaka [-h] -s SCRIPT [-c COUNT] [-t TEMPLATE] + +bash cat with a pipe + +options: + -h, --help show this help message and exit + -s, --script SCRIPT script to run in shell + -c, --count COUNT instances count + -t, --template TEMPLATE + template placeholder for instance id substitution +``` + +This tool can spawn N processes running specified script and substitute instance number in specific template `{{template}}` or other, if set. diff --git a/zaebaka.py b/zaebaka.py new file mode 100644 index 0000000..9f5eb67 --- /dev/null +++ b/zaebaka.py @@ -0,0 +1,65 @@ +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()