69 lines
1.7 KiB
Python
69 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
|
|
from sys import argv
|
|
from subprocess import check_output as sh
|
|
|
|
def usage():
|
|
print('Usage:', argv[0], '-lks', '[<query>]')
|
|
print(' ', ' -l', ' list')
|
|
print(' ', ' -k', ' kill')
|
|
print(' ', ' -s', ' start')
|
|
print(' ', ' -r', ' redo, same as -ks')
|
|
print(' ', '<query>', ' default is "dtach", cannot start with "-"')
|
|
exit(1)
|
|
|
|
m = set()
|
|
qs = []
|
|
|
|
for arg in argv[1:]:
|
|
if arg[0] == '-':
|
|
for c in arg[1:]:
|
|
if c == 'r':
|
|
m.add('k')
|
|
m.add('s')
|
|
elif c not in 'lsk':
|
|
usage()
|
|
m.add(c)
|
|
else:
|
|
qs.append(arg)
|
|
|
|
if not m:
|
|
usage()
|
|
|
|
if not qs:
|
|
qs.append('dtach')
|
|
|
|
basedir = '/home/zgrep/offtopiabday/'
|
|
|
|
lines = []
|
|
with open(basedir + 'starter') as fh:
|
|
for line in fh:
|
|
if all(q in line for q in qs):
|
|
lines.append(line.strip())
|
|
|
|
pids = []
|
|
|
|
if 'l' in m or 'k' in m:
|
|
print('Currently running:')
|
|
modlin = [' '.join((x[1:].lstrip() if x[0] == '#' else x).split()[:3]) for x in lines]
|
|
out = sh(['ps']).decode().split('\n')[1:]
|
|
for line in out:
|
|
if any(l in line for l in modlin) and './redo ' not in line:
|
|
print(' ', line)
|
|
pids.append(line.split()[0])
|
|
|
|
if pids and 'k' in m:
|
|
print('\nKill the above? (Y/N)')
|
|
if input().strip().lower() != 'y':
|
|
print('Not killing; giving up.')
|
|
exit(0)
|
|
|
|
for pid in pids:
|
|
print('Killing', pid, end='.\n')
|
|
sh(['kill', pid])
|
|
|
|
if 's' in m:
|
|
for line in lines:
|
|
if line[0] != '#':
|
|
print('Starting', line, end='.\n')
|
|
sh(line, shell=True)
|