Skip to content

Instantly share code, notes, and snippets.

@pinguet62
Created July 11, 2019 08:01

Revisions

  1. pinguet62 created this gist Jul 11, 2019.
    39 changes: 39 additions & 0 deletions start-wait-test-stop.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    import argparse
    import os
    import signal
    import subprocess
    import urllib2

    parser = argparse.ArgumentParser()
    parser.add_argument('--start', required=True)
    parser.add_argument('--wait', required=True)
    parser.add_argument('--test', required=True)
    args = parser.parse_args()
    start = args.start
    wait = args.wait
    test = args.test

    # Start
    startProcess = subprocess.Popen(start, shell=True)
    signal.signal(signal.SIGINT, lambda signum, frame: os.kill(startProcess.pid, signum))
    signal.signal(signal.SIGTERM, lambda signum, frame: os.kill(startProcess.pid, signum))

    # Wait
    while True:
    try:
    urllib2.urlopen(wait)
    break
    except:
    pass

    try:
    # Test
    subprocess.check_call(test, shell=True)
    finally:
    # Stop
    os.kill(startProcess.pid, signal.SIGTERM)
    startProcess.kill()
    startProcess.communicate()