import os, signal, sys, datetime, json
pid = int(sys.argv[1])
def descendants(p):
    try: children = open(f'/proc/{p}/task/{p}/children').read().split()
    except FileNotFoundError: return []
    result = []
    for c in children: result += descendants(int(c)) + [int(c)]
    return result
pids = [pid] + descendants(pid)
for p in pids:
    try: os.kill(p, signal.SIGCONT)
    except ProcessLookupError: pass
with open(sys.argv[2], 'a') as f:
    f.write(json.dumps({'event': 'resume', 'utc': datetime.datetime.now(datetime.timezone.utc).isoformat(), 'reason': 'sleeping driver received SIGTERM; resumed retained server tree manually'}) + '\n')
print('resumed', len(pids), 'processes')
