#!/usr/bin/env python

import functools
import os
import signal
import sys

import docker_utils


def handler(name, signum, frame):
    print('Should kill', name)
    try:
        docker_utils.client.remove_container(name, force=True)
    except Exception:
        pass
    sys.exit(0)


def launch(name, args):
    try:
        os.unlink('/child_{}'.format(name))
    except OSError:
        pass
    try:
        docker_utils.client.remove_container(name, force=True)
    except Exception:
        pass
    args.insert(0, '--cidfile=/child_{}'.format(name))
    docker_utils.docker('run', *args)


if __name__ == '__main__':
    name = sys.argv[1]
    args = sys.argv[2:]
    signal.signal(signal.SIGINT, functools.partial(handler, name))
    launch(name, args)

#!/usr/bin/env python

import functools
import os
import signal
import sys

import docker_utils


def handler(name, signum, frame):
    print('Should kill', name)
    try:
        cid = open('/child_{}'.format(name)).read().strip()
        docker_utils.client.remove_container(cid, force=True)
    except Exception:
        pass
    sys.exit(0)


def launch(name, args):
    try:
        cid = open('/child_{}'.format(name)).read().strip()
    except IOError:
        cid = name
    try:
        os.unlink('/child_{}'.format(name))
    except OSError:
        pass
    try:
        docker_utils.client.remove_container(cid, force=True)
    except Exception:
        pass
    args.insert(0, '--cidfile=/child_{}'.format(name))
    docker_utils.docker('run', *args)


if __name__ == '__main__':
    name = sys.argv[1]
    args = sys.argv[2:]
    signal.signal(signal.SIGINT, functools.partial(handler, name))
    launch(name, args)

Remove children via uid rather than name