def execute_save_output(cmd, file_name, **kwargs):
    kwargs = kwargs.copy()
    mkdirslist(dirname(file_name))
    try:
        with open(file_name, 'wb') as fh:
            return execute(cmd, stdout_fh=fh, stderr_fh=fh, **kwargs)
    except excp.ProcessExecutionError:
        with excp.reraise():
            try:
                with open(file_name, 'rb') as fh:
                    lines = collections.deque(fh,
                                              maxlen=_TRUNCATED_OUTPUT_LINES)
                content = "".join(lines)
            except IOError:
                pass
            else:
                LOG.debug('Last lines from %s:\n%s', file_name, content)
Helper method to execute a command through subprocess and save stdout
dataset/ETHPy150Open openstack/anvil/anvil/shell.py/execute_save_output