This commit is contained in:
pegasko 2024-11-27 20:06:59 +03:00
commit 019ad02c6a
2 changed files with 88 additions and 0 deletions

23
README.md Normal file
View file

@ -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.

65
zaebaka.py Normal file
View file

@ -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()