'Executes a command.
Parameters
realCmd : str
The command to execute.
timeout : int
The time out in seconds for the command.
shell : bool
If shell is True (default), the specified command (may be a string) will be
executed through the shell.
output : bool
If output is True, the function returns tuple (success, stdoutdata, stderrdata, returncode).
If False, just indication of success is returned
varsDict: dict
variables supplied to the command (or to the shell script)
Returns
bool or (bool, str, str, int)
True if the command succeeded and with stdout, stderr, returncode if output was set to True
Raises
OSError
If command fails to be executed.
RuntimeError
If command execution times out.'

@staticmethod
def executeCmd(realCmd, timeout=60, shell=True, output=False, tout_kill_tree=True, success_codes=(0,), varsDict=None):

    stdout = stderr = None
    retcode = None
    popen = env = None
    if varsDict:
        if shell:
            realCmd = Utils.buildShellCmd(realCmd, varsDict)
        else:
            env = _merge_dicts(os.environ, varsDict)
    realCmdId = id(realCmd)
    logCmd = (lambda level: logSys.log(level, '%x    --    exec:    %s', realCmdId, realCmd))
    try:
        popen = subprocess.Popen(realCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell, env=env, preexec_fn=os.setsid)
        retcode = popen.poll()
        if (retcode is None):
            def _popen_wait_end():
                retcode = popen.poll()
                return ((True, retcode) if (retcode is not None) else None)
            retcode = Utils.wait_for(_popen_wait_end, timeout, Utils.DEFAULT_SHORTEST_INTERVAL)
            if retcode:
                retcode = retcode[1]
        if (retcode is None):
            if logCmd:
                logCmd(logging.ERROR)
                logCmd = None
            logSys.error(('%x    --    timed    out    after    %s    seconds.' % (realCmdId, timeout)))
            pgid = os.getpgid(popen.pid)
            os.killpg(pgid, signal.SIGTERM)
            time.sleep(Utils.DEFAULT_SLEEP_INTERVAL)
            retcode = popen.poll()
            if ((retcode is None) or tout_kill_tree):
                os.killpg(pgid, signal.SIGKILL)
                time.sleep(Utils.DEFAULT_SLEEP_INTERVAL)
                if (retcode is None):
                    retcode = popen.poll()
            if ((retcode is None) and (not Utils.pid_exists(pgid))):
                retcode = signal.SIGKILL
    except OSError as e:
        if logCmd:
            logCmd(logging.ERROR)
            logCmd = None
        stderr = ('%s    --    failed    with    %s' % (realCmd, e))
        logSys.error(stderr)
        if (not popen):
            return (False if (not output) else (False, stdout, stderr, retcode))
    std_level = (logging.DEBUG if (retcode in success_codes) else logging.ERROR)
    if (std_level > logSys.getEffectiveLevel()):
        if logCmd:
            logCmd((std_level - 1))
            logCmd = None
    if (output or (std_level >= logSys.getEffectiveLevel())):
        if popen.stdout:
            try:
                if ((retcode is None) or (retcode < 0)):
                    Utils.setFBlockMode(popen.stdout, False)
                stdout = popen.stdout.read()
            except IOError as e:
                logSys.error('    ...    --    failed    to    read    stdout    %s', e)
            if ((stdout is not None) and (stdout != '') and (std_level >= logSys.getEffectiveLevel())):
                for l in stdout.splitlines():
                    logSys.log(std_level, '%x    --    stdout:    %r', realCmdId, uni_decode(l))
            popen.stdout.close()
        if popen.stderr:
            try:
                if ((retcode is None) or (retcode < 0)):
                    Utils.setFBlockMode(popen.stderr, False)
                stderr = popen.stderr.read()
            except IOError as e:
                logSys.error('    ...    --    failed    to    read    stderr    %s', e)
            if ((stderr is not None) and (stderr != '') and (std_level >= logSys.getEffectiveLevel())):
                for l in stderr.splitlines():
                    logSys.log(std_level, '%x    --    stderr:    %r', realCmdId, uni_decode(l))
            popen.stderr.close()
    success = False
    if (retcode in success_codes):
        logSys.debug('%x    --    returned    successfully    %i', realCmdId, retcode)
        success = True
    elif (retcode is None):
        logSys.error('%x    --    unable    to    kill    PID    %i', realCmdId, popen.pid)
    elif ((retcode < 0) or (retcode > 128)):
        sigcode = ((- retcode) if (retcode < 0) else (retcode - 128))
        logSys.error('%x    --    killed    with    %s    (return    code:    %s)', realCmdId, signame.get(sigcode, ('signal    %i' % sigcode)), retcode)
    else:
        msg = _RETCODE_HINTS.get(retcode, None)
        logSys.error('%x    --    returned    %i', realCmdId, retcode)
        if msg:
            logSys.info('HINT    on    %i:    %s', retcode, (msg % locals()))
    if output:
        return (success, stdout, stderr, retcode)
    return (success if (len(success_codes) == 1) else (success, retcode))
