Execute a command with timeout, and return STDOUT and STDERR Args: cmd(str): the command to execute. timeout(float): timeout in seconds. Returns: output(bytes), retcode(int). If timeout, retcode is -1.
(cmd, timeout=None)
| 241 | |
| 242 | |
| 243 | def subproc_call(cmd, timeout=None): |
| 244 | """ |
| 245 | Execute a command with timeout, and return STDOUT and STDERR |
| 246 | |
| 247 | Args: |
| 248 | cmd(str): the command to execute. |
| 249 | timeout(float): timeout in seconds. |
| 250 | |
| 251 | Returns: |
| 252 | output(bytes), retcode(int). If timeout, retcode is -1. |
| 253 | """ |
| 254 | try: |
| 255 | output = subprocess.check_output( |
| 256 | cmd, stderr=subprocess.STDOUT, |
| 257 | shell=True, timeout=timeout) |
| 258 | return output, 0 |
| 259 | except subprocess.TimeoutExpired as e: |
| 260 | logger.warn("Command '{}' timeout!".format(cmd)) |
| 261 | if e.output: |
| 262 | logger.warn(e.output.decode('utf-8')) |
| 263 | return e.output, -1 |
| 264 | else: |
| 265 | return "", -1 |
| 266 | except subprocess.CalledProcessError as e: |
| 267 | logger.warn("Command '{}' failed, return code={}".format(cmd, e.returncode)) |
| 268 | logger.warn(e.output.decode('utf-8')) |
| 269 | return e.output, e.returncode |
| 270 | except Exception: |
| 271 | logger.warn("Command '{}' failed to run.".format(cmd)) |
| 272 | return "", -2 |
| 273 | |
| 274 | |
| 275 | class OrderedContainer(object): |
no test coverage detected