'Run cmd in a shell.
cmd may be a string, which is parsed by shlex.split, or a list (similar
to subprocess.Popen).
Examples
spawn("firefox")
spawn(["xterm", "-T", "Temporary terminal"])'

def cmd_spawn(self, cmd):

    if isinstance(cmd, six.string_types):
        args = shlex.split(cmd)
    else:
        args = list(cmd)
    (r, w) = os.pipe()
    pid = os.fork()
    if (pid < 0):
        os.close(r)
        os.close(w)
        return pid
    if (pid == 0):
        os.close(r)
        os.close(0)
        os.close(1)
        os.close(2)
        pid2 = os.fork()
        if (pid2 == 0):
            os.close(w)
            try:
                fd = os.open(os.devnull, os.O_RDWR)
            except OSError:
                pass
            else:
                try:
                    os.set_inheritable(fd, True)
                except AttributeError:
                    pass
                if (fd > 0):
                    os.dup2(fd, 0)
                os.dup2(fd, 1)
                os.dup2(fd, 2)
            try:
                os.execvp(args[0], args)
            except OSError as e:
                logger.error('failed    spawn:    "{0}"\n{1}'.format(cmd, e))
            os._exit(1)
        else:
            os.write(w, str(pid2).encode())
            os.close(w)
            os._exit(0)
    else:
        os.close(w)
        os.waitpid(pid, 0)
        pid = os.read(r, 1024)
        os.close(r)
        return int(pid)
