def exec_cmd(cmd, env=None): """ Execute cmd in an external process, collect its output and returncode :param cmd: an iterator of strings to be passed as exec(2)'s argv :param env: an optional dictionary to be placed as environment variables of the external process. If None, the environment of the calling process is used. :returns: a 3-tuple of the process's (returncode, stdout content, stderr content.) This is a bare-bones version of `commands.execCmd`. Unlike the latter, this function * uses Vdsm cpu pinning, and must not be used for long CPU-bound processes. * does not guarantee to kill underlying process if CPopen.communicate() raises. Commands that access shared storage may not use this api. * does not hide passwords in logs if they are passed in cmd """ logging.debug(command_log_line(cmd)) p = CPopen( cmd, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) out, err = p.communicate() logging.debug(retcode_log_line(p.returncode, err=err)) return p.returncode, out, err
def execCmd(command, sudo=False, cwd=None, data=None, raw=False, printable=None, env=None, sync=True, nice=None, ioclass=None, ioclassdata=None, setsid=False, execCmdLogger=logging.root, deathSignal=None, resetCpuAffinity=True): """ Executes an external command, optionally via sudo. IMPORTANT NOTE: the new process would receive `deathSignal` when the controlling thread dies, which may not be what you intended: if you create a temporary thread, spawn a sync=False sub-process, and have the thread finish, the new subprocess would die immediately. """ command = cmdutils.wrap_command(command, with_ioclass=ioclass, ioclassdata=ioclassdata, with_nice=nice, with_setsid=setsid, with_sudo=sudo, reset_cpu_affinity=resetCpuAffinity) # Unsubscriptable objects (e.g. generators) need conversion if not callable(getattr(command, '__getitem__', None)): command = tuple(command) if not printable: printable = command execCmdLogger.debug(command_log_line(printable, cwd=cwd)) extra = {} extra['stderr'] = subprocess.PIPE extra['stdout'] = subprocess.PIPE if deathSignal is not None: extra['deathSignal'] = deathSignal p = CPopen(command, close_fds=True, cwd=cwd, env=env, **extra) if not sync: p = AsyncProc(p) if data is not None: p.stdin.write(data) p.stdin.flush() return p with terminating(p): (out, err) = p.communicate(data) if out is None: # Prevent splitlines() from barfing later on out = "" execCmdLogger.debug(retcode_log_line(p.returncode, err=err)) if not raw: out = out.splitlines(False) err = err.splitlines(False) return p.returncode, out, err
def _child_processes(self): proc = CPopen(self.PGREP_CMD, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate() # EXIT STATUS # 0 One or more processes matched the criteria. # 1 No processes matched. if proc.returncode not in (0, 1): raise RuntimeError("Error running pgrep: [%d] %s" % (proc.returncode, err)) return frozenset(int(pid) for pid in out.splitlines())
def exec_sync(cmds): logging.debug(cmdutils.command_log_line(cmds)) p = Popen( cmds, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() logging.debug(cmdutils.retcode_log_line(p.returncode, err=err)) return p.returncode, out, err
def exec_sync_bytes(cmds): logging.debug(cmdutils.command_log_line(cmds)) p = Popen(cmds, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() logging.debug(cmdutils.retcode_log_line(p.returncode, err=err)) return p.returncode, out, err