def run_command()

in VMExtension/Utils/ScriptUtil.py [0:0]


def run_command(hutil, args, cwd, operation, extension_short_name, version, exit_after_run=True, interval=30,
                std_out_file_name=DefaultStdoutFile, std_err_file_name=DefaultErroutFile):
    std_out_file = os.path.join(cwd, std_out_file_name)
    err_out_file = os.path.join(cwd, std_err_file_name)
    std_out = None
    err_out = None
    try:
        std_out = open(std_out_file, "w")
        err_out = open(err_out_file, "w")
        start_time = time.time()
        child = subprocess.Popen(args,
                                 cwd=cwd,
                                 stdout=std_out,
                                 stderr=err_out)
        time.sleep(1)
        while child.poll() is None:
            msg = "Command is running..."
            msg_with_cmd_output = LogUtil.get_formatted_log(msg, LogUtil.tail(std_out_file), LogUtil.tail(err_out_file))
            msg_without_cmd_output = msg + " Stdout/Stderr omitted from output."

            hutil.log_to_file(msg_with_cmd_output)
            hutil.log_to_console(msg_without_cmd_output)
            hutil.do_status_report(operation, 'transitioning', '0', msg_without_cmd_output)
            time.sleep(interval)

        exit_code = child.returncode
        if child.returncode and child.returncode != 0:
            msg = "Command returned an error."
            msg_with_cmd_output = LogUtil.get_formatted_log(msg, LogUtil.tail(std_out_file), LogUtil.tail(err_out_file))
            msg_without_cmd_output = msg + " Stdout/Stderr omitted from output."

            hutil.error(msg_without_cmd_output)
            waagent.AddExtensionEvent(name=extension_short_name,
                                      op=operation,
                                      isSuccess=False,
                                      version=version,
                                      message="(01302)" + msg_without_cmd_output)
        else:
            msg = "Command is finished."
            msg_with_cmd_output = LogUtil.get_formatted_log(msg, LogUtil.tail(std_out_file), LogUtil.tail(err_out_file))
            msg_without_cmd_output = msg + " Stdout/Stderr omitted from output."

            hutil.log_to_file(msg_with_cmd_output)
            hutil.log_to_console(msg_without_cmd_output)
            waagent.AddExtensionEvent(name=extension_short_name,
                                      op=operation,
                                      isSuccess=True,
                                      version=version,
                                      message="(01302)" + msg_without_cmd_output)
            end_time = time.time()
            waagent.AddExtensionEvent(name=extension_short_name,
                                      op=operation,
                                      isSuccess=True,
                                      version=version,
                                      message=("(01304)Command execution time: "
                                               "{0}s").format(str(end_time - start_time)))

        log_or_exit(hutil, exit_after_run, exit_code, operation, msg_with_cmd_output)
    except Exception as e:
        error_msg = ("Failed to launch command with error: {0},"
                     "stacktrace: {1}").format(e, traceback.format_exc())
        hutil.error(error_msg)
        waagent.AddExtensionEvent(name=extension_short_name,
                                  op=operation,
                                  isSuccess=False,
                                  version=version,
                                  message="(01101)" + error_msg)
        exit_code = 1
        msg = 'Launch command failed: {0}'.format(e)

        log_or_exit(hutil, exit_after_run, exit_code, operation, msg)
    finally:
        if std_out:
            std_out.close()
        if err_out:
            err_out.close()
    return exit_code