import subprocess, time, sys
pat = sys.argv[1] if len(sys.argv) > 1 else 'sf_delivery_watchdog'
max_s = int(sys.argv[2]) if len(sys.argv) > 2 else 420
t0 = time.time()
while time.time() - t0 < max_s:
    r = subprocess.run(['pgrep', '-f', pat], capture_output=True, text=True)
    if r.returncode != 0 or not r.stdout.strip():
        print('EXITED/ABSENT after %.0fs' % (time.time() - t0))
        break
    time.sleep(5)
else:
    print('STILL RUNNING after %.0fs' % (time.time() - t0))
r = subprocess.run(['pgrep', '-fl', pat], capture_output=True, text=True)
print('pgrep result:', (r.stdout or '(none)').strip())
print('time:', time.strftime('%Y-%m-%d %H:%M:%S'))
