我们从Python开源项目中,提取了以下48个代码示例,用于说明如何使用subprocess.TimeoutExpired()。
def run(self): """Runs the autotest. Diff will be stored in self.diff""" process = subprocess.Popen([self.binary] + self.args, stdin=subprocess.PIPE, stdout=subprocess.PIPE) for line in self.input: process.stdin.write(line.encode()) try: out, err = process.communicate(timeout=self.time_limit) # TODO test timeout if err: self.diff = """Encountered error running test: Output: {} Error output: {} """.format(out.decode(), err.decode()) else: self.diff = diff(out.decode(), self.expected) # TODO make sure I didn't break diff except subprocess.TimeoutExpired: self.diff = colours['red']('Time limit exceeded.')
def _compile_proto(full_path, dest): 'Helper to compile protobuf files' proto_path = os.path.dirname(full_path) protoc_args = [find_protoc(), '--python_out={}'.format(dest), '--proto_path={}'.format(proto_path), full_path] proc = subprocess.Popen(protoc_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: outs, errs = proc.communicate(timeout=5) except subprocess.TimeoutExpired: proc.kill() outs, errs = proc.communicate() return False if proc.returncode != 0: msg = 'Failed compiling "{}": \n\nstderr: {}\nstdout: {}'.format( full_path, errs.decode('utf-8'), outs.decode('utf-8')) raise BadProtobuf(msg) return True
def my_thread(): global files,path,timeout,options myname= threading.currentThread().getName() while files: #create command to run nextfile=files.pop() #print name of thread and command being run print('Thread {0} starts processing {1}'.format(myname,nextfile)) f=path + nextfile + options try: #timeout interrupts frozen command, shell=True does'nt open a console subprocess.check_call(args= f , shell=True, timeout=timeout) except subprocess.TimeoutExpired: print('Thread {0} Processing {0} took too long' .format(myname,nextfile)) except subprocess.CalledProcessError as e: print ('Thread {0} Processing {1} returned error {2}:{3}'.format(myname,nextfile,e.returncode,e.output)) except Exception as e: print ('Thread {0} Processing {1} returned error {2}'.format(myname,nextfile,type(e).__name__)) print ('thread {0} stopped'.format(myname))
def runCmd(cmd,cmd_timeout=300): ''' run command without showing console window on windows - return stdout and stderr as strings ''' startupinfo = None output = "" output_err = "" debug_log("runCmd: {}".format(cmd)) if os.name == 'nt': startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW try: proc = subprocess.Popen(cmd,bufsize=-1,startupinfo=startupinfo,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=None,shell=False,universal_newlines=False) except SubprocessError as e: proc = None debug_log("exception in runCmd: {}".format(e),logging.ERROR) if proc is not None: try: outputb, output_errb = proc.communicate() output = outputb.decode('utf-8','replace') output_err = output_errb.decode('utf-8','replace') except subprocess.TimeoutExpired(timeout=cmd_timeout): proc.kill() debug_log("runCmd: Process killed due to timeout",logging.WARNING) else: debug_log("runCmd: Proc was none",logging.WARNING) return output,output_err
def shutdown(self): """Shut down paired listener with <END> signal.""" if hasattr(self, 'socket'): try: self.socket.send_unicode('<END>', zmq.NOBLOCK) except zmq.error.ZMQError: # may need to listen first try: self.socket.recv_unicode(zmq.NOBLOCK) self.socket.send_unicode('<END>', zmq.NOBLOCK) except zmq.error.ZMQError: # paired process is probably dead already pass if hasattr(self, 'process'): # try to let the subprocess clean up, but don't wait too long try: self.process.communicate(timeout=1) except subprocess.TimeoutExpired: self.process.kill()
def adb_pushfile(adb, filepath, remote_path): filesize = os.path.getsize(filepath) pb = tqdm.tqdm(unit='B', unit_scale=True, total=filesize) p = adb.raw_cmd('push', filepath, remote_path) while True: try: p.wait(0.5) except subprocess.TimeoutExpired: pb.n = get_file_size(adb, remote_path) pb.refresh() # log.info("Progress %dM/%dM", get_file_size(remote_path) >>20, filesize >>20) pass except (KeyboardInterrupt, SystemExit): p.kill() raise except: raise else: # log.info("Success pushed into device") break pb.close()
def exec_shell_command( self, command ): output = None try: output = subprocess.check_output( command, shell=True, timeout=50 ) output = output.strip().decode('utf-8') except subprocess.CalledProcessError: """ _LOGGER.error("Command failed: %s", command)""" self.value = CONST_STATE_ERROR output = None except subprocess.TimeoutExpired: """ _LOGGER.error("Timeout for command: %s", command)""" self.value = CONST_STATE_ERROR output = None if output == None: _LOGGER.error( "Life360 has not responsed well. Nothing to worry, will try again!" ) self.value = CONST_STATE_ERROR return None else: return output
def runTarsnap(args, timeout = None): command = [config.tarsnap_bin] + config.tarsnap_extra_args + args proc = subprocess.Popen(command, stdout = subprocess.PIPE, stderr = subprocess.PIPE, stdin = subprocess.DEVNULL, universal_newlines = True) result = CheapoCompletedProcess() try: result.stdout, result.stderr = proc.communicate(timeout = timeout) result.returncode = proc.wait() if result.returncode: sys.exit("Error running tarsnap:\nCommand: {}\nSTDOUT:\n{}\nSTDERR:\n{}\n".format(" ".join(command),result.stdout,result.stderr)) return result except subprocess.TimeoutExpired: print("Tarsnap timed out, sending SIGQUIT...") proc.send_signal(signal.SIGQUIT) result.stdout, result.stderr = proc.communicate() result.returncode = proc.wait() print("Tarsnap finished") if result.returncode: sys.exit("Error running tarsnap:\nCommand: {}\nSTDOUT:\n{}\nSTDERR:\n{}\n".format(" ".join(command),result.stdout,result.stderr)) return result
def sh(self, ctx, *, command): """Execute a system command. Bot owner only.""" command = command.split(" ") process = subprocess.Popen(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: output, errors = process.communicate(timeout=8) output = output.split("\n") process.terminate() except subprocess.TimeoutExpired: process.kill() output = ["Command timed out. x.x"] paginator = commands.Paginator(prefix="```bash") for line in output: paginator.add_line(line) for page in paginator.pages: await ctx.send(page)
def install(self, path): logger.info("Installing npm packages...") process = subprocess.Popen( ["npm", "install"], cwd=path, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) try: return_code = process.wait() except subprocess.TimeoutExpired: return True if return_code is not 0: raise Exception("Return code was non-zero") logger.info("Done.") return True
def __read_process_stream(self, proc, kill_after=None): timedOut = False outS = "" errS = "" try: outs, errs = proc.communicate(timeout=kill_after) outS = outs.decode() errS = errs.decode() except subprocess.TimeoutExpired as e: proc.terminate() proc.wait() if e.stdout is not None: outS = e.stdout.decode() if e.stderr is not None: errS = e.stderr.decode() timedOut = True logger.debug("%s was terminated", str(" ".join(proc.args))) return (outS, errS, timedOut)
def close(self): """Close any open window. Note that this only works with non-blocking methods. """ if self._process: # Be nice first. self._process.send_signal(signal.SIGINT) # If it doesn't close itself promptly, be brutal. try: self._process.wait(timeout=1) except subprocess.TimeoutExpired: self._process.send_signal(signal.SIGKILL) # Clean up. self._process = None
def _run(*args, env=None, check=False, timeout=None): with subprocess.Popen([a.encode('utf-8') for a in args], env=env, stdout=PIPE, stderr=PIPE) as process: try: stdout, stderr = process.communicate(input, timeout=timeout) except TimeoutExpired: process.kill() stdout, stderr = process.communicate() raise TimeoutExpired( process.args, timeout, output=stdout, stderr=stderr, ) except: process.kill() process.wait() raise retcode = process.poll() if check and retcode: raise subprocess.CalledProcessError( retcode, process.args, output=stdout, stderr=stderr, ) return subprocess.CompletedProcess(process.args, retcode, stdout, stderr)
def sigint_handler(sig, frame): print("Killing sub-process...") if process_handle is not None: global kill_retry_count while process_handle.returncode is None and kill_retry_count < kill_retry_max: kill_retry_count += 1 print("Killing sub-process ({})...".format(kill_retry_count)) try: os.killpg(os.getpgid(process_handle.pid), signal.SIGTERM) os.waitpid(process_handle.pid, os.WNOHANG) except ProcessLookupError: break try: process_handle.wait(1) except subprocess.TimeoutExpired: pass if working_dir is not None: rmdir(working_dir) sys.exit(0)
def run_shell_command(cmd, cwd=None, timeout=15): proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) out = [] err = [] try: proc.wait(timeout=timeout) except subprocess.TimeoutExpired: kill(proc.pid) for line in proc.stdout.readlines(): out.append(line.decode()) for line in proc.stderr.readlines(): err.append(line) return out, err, proc.pid
def close(self): ''' Terminate the controller process and it's child processes. Args: - None ''' if self.reuse : logger.debug("Ipcontroller not shutting down: reuse enabled") return try: pgid = os.getpgid(self.proc.pid) status = os.killpg(pgid, signal.SIGTERM) time.sleep(0.2) os.killpg(pgid, signal.SIGKILL) try: self.proc.wait(timeout=1) x = self.proc.returncode logger.debug("Controller exited with {0}".format(x)) except subprocess.TimeoutExpired : logger.warn("Ipcontroller process:{0} cleanup failed. May require manual cleanup".format(self.proc.pid)) except Exception as e: logger.warn("Failed to kill the ipcontroller process[{0}]: {1}".format(self.proc.pid, e))
def wait_for_standing_subprocess(proc, timeout=None): """Waits for a subprocess started by start_standing_subprocess to finish or times out. Propagates the exception raised by the subprocess.wait(.) function. The subprocess.TimeoutExpired exception is raised if the process timed-out rather then terminating. If no exception is raised: the subprocess terminated on its own. No need to call stop_standing_subprocess() to kill it. If an exception is raised: the subprocess is still alive - it did not terminate. Either call stop_standing_subprocess() to kill it, or call wait_for_standing_subprocess() to keep waiting for it to terminate on its own. Args: p: Subprocess to wait for. timeout: An integer number of seconds to wait before timing out. """ proc.wait(timeout)
def exec(command: str, cwd: str = os.getcwd(), logger=logging.getLogger(__name__), timeout: Optional[int] = None): logger.debug("Execute '%s' in '%s'", command, cwd) encoding = Shell.__get_encoding() try: # On our Debian server subprocess does not return until after the process finished, but then correctly # raises TimeoutExpired, if the process took to long. We use `timeout` to ensure that the process terminates # eventually. if "Linux" in platform() and timeout is not None: command = "timeout {} {}".format(timeout + 60, command) output = Shell.__exec(command, cwd, timeout, encoding) logger.debug(output) return output except CalledProcessError as e: raise CommandFailedError(e.cmd, e.output.decode(encoding), e.stderr.decode(encoding)) except TimeoutExpired as e: raise TimeoutError(e.cmd, e.output.decode(encoding))
def test_communicate_timeout(self): p = subprocess.Popen([sys.executable, "-c", 'import sys,os,time;' 'sys.stderr.write("pineapple\\n");' 'time.sleep(1);' 'sys.stderr.write("pear\\n");' 'sys.stdout.write(sys.stdin.read())'], universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.assertRaises(subprocess.TimeoutExpired, p.communicate, "banana", timeout=0.3) # Make sure we can keep waiting for it, and that we get the whole output # after it completes. (stdout, stderr) = p.communicate() self.assertEqual(stdout, "banana") self.assertStderrEqual(stderr.encode(), b"pineapple\npear\n")
def test_communicate_timeout_large_ouput(self): # Test an expiring timeout while the child is outputting lots of data. p = subprocess.Popen([sys.executable, "-c", 'import sys,os,time;' 'sys.stdout.write("a" * (64 * 1024));' 'time.sleep(0.2);' 'sys.stdout.write("a" * (64 * 1024));' 'time.sleep(0.2);' 'sys.stdout.write("a" * (64 * 1024));' 'time.sleep(0.2);' 'sys.stdout.write("a" * (64 * 1024));'], stdout=subprocess.PIPE) self.assertRaises(subprocess.TimeoutExpired, p.communicate, timeout=0.4) (stdout, _) = p.communicate() self.assertEqual(len(stdout), 4 * 64 * 1024) # Test for the fd leak reported in http://bugs.python.org/issue2791.
def exec_command(command, timeout=None, **kwargs): try: proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=sys.stderr, **kwargs) except FileNotFoundError: log.error('No such file or directory: %s', command) sys.exit(1) except PermissionError: log.error('Permission denied: %s', command) sys.exit(1) try: answer, _ = proc.communicate(timeout=timeout) except subprocess.TimeoutExpired: answer = b'' return answer, proc # We should use this instead of posixpath.normpath # posixpath.normpath doesn't collapse a leading duplicated slashes. see: https://stackoverflow.com/questions/7816818/why-doesnt-os-normpath-collapse-a-leading-double-slash
def _wait_xena_2544_complete(self): """ Wait for Xena2544.exe completion. :return: None """ data = '' while True: try: self.mono_pipe.wait(60) self._log_handle.close() break except subprocess.TimeoutExpired: # check the log to see if Xena2544 has completed and mono is # deadlocked. data += self._log_handle.read() if 'TestCompletedSuccessfully' in data: self._log_handle.close() self.mono_pipe.terminate() break
def stop(self): """ Stop process if it's running. """ if self.cleaned: raise ValueError("Can't call stop on process after cleanup was performed.") if self.poll() is None: # process is running self.terminate() try: logger.debug('Waiting for {} process to terminate.'.format(type(self).__name__)) self.wait(timeout=10) except subprocess.TimeoutExpired: self.kill() logger.warning('Process {} killed after unsuccessful termination.'.format(type(self).__name__)) else: logger.debug('Process {} terminated.'.format(type(self).__name__)) self.update()
def update(self, ctx): """ Updates the bot from the Github repo """ await ctx.send("Calling process to update! :up: :date: ") try: done = subprocess.run("git pull", shell=True, stdout=subprocess.PIPE, timeout=30) if done: message = done.stdout.decode() await ctx.send("`{}`".format(message)) if message == "Already up-to-date.\n": await ctx.send("No update available :no_entry:") else: await ctx.send("Succesfully updated! Rebooting now :repeat: ") await self.bot.logout() except subprocess.CalledProcessError: await ctx.send("Error updating! :exclamation: ") except subprocess.TimeoutExpired: await ctx.send("Error updating - Process timed out! :exclamation: ")
def do_install(): info('Starting up VM for unattended install') vm_proc = sh_spawn( 'bhyve -m ${MEMSIZE} -c ${CORES} -A -H -P', '-s 3:0,ahci-hd,${destdir}/boot.img', '-s 4:0,ahci-hd,${destdir}/hd1.img', '-s 5:0,ahci-hd,${destdir}/hd2.img', '-s 6:0,ahci-cd,${isopath}', '-s 7:0,virtio-net,${tapdev}', '-s 8:0,fbuf,tcp=5900,w=1024,h=768', '-s 31,lpc', '-l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI.fd', '${VM_NAME}' ) try: vm_proc.wait(timeout=3600) except subprocess.TimeoutExpired: fail('Install timed out after 1 hour')
def close_fs(t): import test_fs (p, args) = t args = test_fs.argument_parser().parse_args(args) try: subprocess.call(['umount', args.mountpoint], stderr=subprocess.DEVNULL) except: pass try: rc = p.wait(timeout=3) except subprocess.TimeoutExpired: p.terminate() try: rc = p.wait(timeout=3) except subprocess.TimeoutExpired: p.kill() p.wait()
def run(cmd, timeout_sec): if (sys.version_info >= (3,5)): try: completed = subprocess.run(shlex.split(cmd), timeout=timeout_sec, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return completed.stdout.decode('utf-8'), completed.stderr.decode('utf-8') except subprocess.TimeoutExpired: message = 'Fitting got killed by timeout after '+str(timeout_sec)+' sec!' print (message) raise EslException(message) else: proc = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE) kill_proc = lambda p: p.kill() timer = Timer(timeout_sec, kill_proc, [proc]) try: timer.start() stdout,stderr = proc.communicate() finally: timer.cancel() if (proc.poll() == -9 ): raise EslException('Fitting probably got killed by timeout!') #if (proc.poll() != 0 ): # raise EslException('Fitting crashed! returned '+str(proc.poll())) return stdout, stderr
def kill_drone(): global DRONE_RUNNING LOG.info('stopping drone') if DRONE_RUNNING is False: LOG.warning('drone is not running, nothing to do') return LOG.info('trying to stop drone gracefully') DRONE.terminate() try: DRONE.wait(timeout=10) LOG.info('drone exited gracefully') except subprocess.TimeoutExpired: LOG.info('could not terminate drone properly, kill it.') DRONE.kill() DRONE.wait(timeout=10) LOG.info('drone has been killed') DRONE_RUNNING = False
def do_onecam(i, offset_ms,iterator): time.sleep(offset_ms/1000.0) print("Time = %f ms : Capturing camera num %d" % (offset_ms, i)) # TODO: run the camera capture code filename = "/media/ubuntu/VRcameraSSD/tmp/cam%d.%d" % (i,iterator) p = subprocess.Popen( ["sudo", "./snapshot", filename, "--dev", ("/dev/still%d" % i), "--format", "jpg", "--size", str(4192), str(3104), "--suspend", "--resume"], stdout=subprocess.PIPE) try: (output, err) = p.communicate(timeout=30) output_str = output.decode("utf-8") if re.search(r"\*FULL\*", output_str, flags=re.MULTILINE): print("OK") elif re.search(r"\*INCOMPLETE\*", output_str, flags=re.MULTILINE): print("INCOMPLETE") else: print("*** FAILED ***") print("Kernel log:") #print(get_kern_log(10)) except subprocess.TimeoutExpired: print("TIMEOUT")
def test_all_cameras(numCams, width, height): for i in range(0, numCams): print(" cam %d : " % i, end="") p = subprocess.Popen( ["sudo", "./snapshot", "/dev/null", "--dev", ("/dev/still%d" % i), "--format", "none", "--size", str(width), str(height), "--suspend", "--resume"], stdout=subprocess.PIPE) try: (output, err) = p.communicate(timeout=30) output_str = output.decode("utf-8") if re.search(r"\*FULL\*", output_str, flags=re.MULTILINE): print("OK") elif re.search(r"\*INCOMPLETE\*", output_str, flags=re.MULTILINE): print("INCOMPLETE") else: print("*** FAILED ***") print("Kernel log:") print(get_kern_log(10)) except subprocess.TimeoutExpired: print("TIMEOUT") # Read an image from each camera and save it to disk.
def do_onecam(i, offset_ms): time.sleep(offset_ms/1000.0) print("Time = %f ms : Capturing camera num %d" % (offset_ms, i)) # TODO: run the camera capture code filename = "/media/ubuntu/VRcameraSSD/tmp/cam%d.yuyv" % (i) p = subprocess.Popen( ["sudo", "./snapshot", filename, "--dev", ("/dev/still%d" % i), "--format", "yuyv", "--size", str(4192), str(3104), "--suspend", "--resume"], stdout=subprocess.PIPE) try: (output, err) = p.communicate(timeout=30) output_str = output.decode("utf-8") if re.search(r"\*FULL\*", output_str, flags=re.MULTILINE): print("OK") elif re.search(r"\*INCOMPLETE\*", output_str, flags=re.MULTILINE): print("INCOMPLETE") else: print("*** FAILED ***") print("Kernel log:") #print(get_kern_log(10)) except subprocess.TimeoutExpired: print("TIMEOUT")
def wait_for_active_job(signal_to_send=None): """ Wait for the active job to finish, to be killed by SIGINT, or to be suspended by ctrl-z. """ _clear_dead_jobs() act = builtins.__xonsh_active_job__ if act is None: return job = builtins.__xonsh_all_jobs__[act] obj = job['obj'] if job['bg']: return while obj.returncode is None: try: obj.wait(0.01) except TimeoutExpired: pass except KeyboardInterrupt: obj.kill() if obj.poll() is not None: builtins.__xonsh_active_job__ = None
def test_cmd_spawning(self): env = os.environ.copy() env["PROXY_API_TOKEN"] = "dummy_token" path = fixtures.get("remoteappmanager_config.py") self.spawner.config_file_path = path args = self.spawner.get_args() try: with self.assertRaises(subprocess.TimeoutExpired): subprocess.check_output( self.spawner.cmd + args, timeout=2, env=env, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as exc: print("Output of the command:\n\n{}".format( exc.output.decode(sys.getdefaultencoding()))) raise
def exec_shell_command( self, command ): output = None try: output = subprocess.check_output( command, shell=True, timeout=60 ) output = output.strip().decode('utf-8') except subprocess.CalledProcessError: """ _LOGGER.error("Command failed: %s", command)""" self.value = CONST_STATE_ERROR output = None except subprocess.TimeoutExpired: """ _LOGGER.error("Timeout for command: %s", command)""" self.value = CONST_STATE_ERROR output = None if output == None: _LOGGER.error( "Life360 has not responsed well. Nothing to worry, will try again!" ) self.value = CONST_STATE_ERROR return None else: return output
def run_subprocess(cinput, invoke, universal_newlines=True, **kwargs): process = subprocess.Popen(invoke, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=universal_newlines, **kwargs) try: result = process.communicate(input=cinput, timeout=60)[0] except subprocess.TimeoutExpired: process.kill() result = "Sorry, your code took too long to run!" partial_out = process.communicate()[0] # communicate returns a tuple first element is stdout second is stderr if partial_out: result += "\nPartial output:\n" + partial_out except: traceback.print_exc() result = "There was an issue running your code." return result #temporary workaround while TIO is bugged
def process(self, cmd=[], input=None, timeout=None): proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.logger.info('Running process, pid=%d: %s' % (proc.pid, str(cmd))) try: output, err = proc.communicate(input, timeout=timeout) except subprocess.TimeoutExpired as e: self.logger.warning( 'Process %d killed with timeout %s' % (proc.pid, str(timeout))) proc.kill() output, err = proc.communicate() self.logger.debug("stdout: " + repr(output)) self.logger.debug("stderr: " + repr(err)) stdout = output.decode('utf-8') stderr = err.decode('utf-8') if proc.returncode != 0: self.logger.warning( 'Process %d failed with rcode %d' % (proc.pid, int(proc.returncode))) else: self.logger.debug('Process %d finished with rcode 0' % (proc.pid)) return int(proc.returncode), stdout, stderr
def run_cmd(self, cmd): """ run a mpd control command """ call = ["mpc"] if self.host: call.extend(["--host", self.host]) if self.port: call.extend(["--port", self.port]) call.append(cmd) proc = subprocess.Popen(call, stdout=subprocess.PIPE) output, _ = proc.communicate() try: proc.wait(1) except subprocess.TimeoutExpired: raise Exception("mpc doesn't terminate!") return output
def _wait_for_containers_exit_status(self, containers): """Wait for all of the specified containers to exit and return their exit codes. Args: containers (list of str): The containers to wait to exit. Returns: list of int: The list of return codes for the process in each container. """ wait = ['docker', 'wait'] + containers handle = subprocess.Popen( args=wait, stdout=subprocess.PIPE, universal_newlines=True) try: output, _ = handle.communicate(timeout=35) return [int(e) for e in output.strip().split('\n')] except subprocess.TimeoutExpired: handle.kill() LOGGER.warning("Docker timed out waiting for %s to exit", containers) return []
def rm(filename, sudo=False): """ Remove a file on the disk, not us os.rm because we want to add timeout to the command. It's possible that the os call got hang when the disk has some problems """ cmd_args = [] if sudo: cmd_args += ['sudo'] cmd_args += ['/bin/rm', filename] log.debug("Executing cmd: {}".format(str(cmd_args))) proc = subprocess.Popen(cmd_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE) try: (stdout, stderr) = proc.communicate(timeout=10) except subprocess.TimeoutExpired: proc.kill() raise OSCError('SHELL_TIMEOUT', {'cmd': ' '.join(cmd_args)})
def update_pools(self): # designate-manage communicates with designate via message bus so no # need to set OS_ vars # NOTE(AJK) this runs with every hook (once most relations are up) and # so if it fails it will be picked up by the next relation change or # update-status. i.e. it will heal eventually. if hookenv.is_leader(): try: cmd = "designate-manage pool update" # Note(tinwood) that this command may fail if the pools.yaml # doesn't actually contain any pools. This happens when the # relation is broken, which errors out the charm. This stops # this happening and logs the error. subprocess.check_call(cmd.split(), timeout=60) except subprocess.CalledProcessError as e: hookenv.log("designate-manage pool update failed: {}" .format(str(e))) except subprocess.TimeoutExpired as e: # the timeout is if the rabbitmq server has gone away; it just # retries continuously; this lets the hook complete. hookenv.log("designate-manage pool command timed out: {}". format(str(e)))
def test_run_script_timed_out_script(self): scripts_dir = self.useFixture(TempDirectory()).path script = make_script(scripts_dir=scripts_dir) self.mock_capture_script_output.side_effect = TimeoutExpired( [factory.make_name('arg') for _ in range(3)], script['timeout_seconds']) self.args.pop('status') self.assertFalse(run_script(script, scripts_dir)) self.assertThat(self.mock_output_and_send, MockCallsMatch( call( 'Starting %s' % script['msg_name'], status='WORKING', **self.args), call( 'Timeout(%s) expired on %s' % ( str(timedelta(seconds=script['timeout_seconds'])), script['msg_name']), files={ script['combined_name']: script['combined'].encode(), script['stdout_name']: script['stdout'].encode(), script['stderr_name']: script['stderr'].encode(), script['result_name']: script['result'].encode(), }, status='TIMEDOUT', **self.args), ))
def _terminate_process(process, wait=2.5, kill=2.5): """Ensures that `process` terminates. :return: The exit code of the process. """ try: # The subprocess may have already been signalled, for example as part # of this process's process group when Ctrl-c is pressed at the # terminal, so give it some time to exit. return process.wait(timeout=wait) except subprocess.TimeoutExpired: # Either the subprocess has not been signalled, or it's slow. process.terminate() # SIGTERM. try: return process.wait(timeout=kill) except subprocess.TimeoutExpired: process.kill() # SIGKILL. return process.wait()
def install(server,*,eula=False): if not os.path.isdir(server.data["dir"]): os.makedirs(server.data["dir"]) mcjar=os.path.join(server.data["dir"],server.data["exe_name"]) if not os.path.isfile(mcjar): raise ServerError("Can't find server jar ({}). Please place the files in the directory and/or update the 'exe_name' then run setup again".format(mcjar)) server.data.save() eulafile=os.path.join(server.data["dir"],"eula.txt") configfile=os.path.join(server.data["dir"],"server.properties") if not os.path.isfile(configfile) or (eula and not os.path.isfile(eulafile)): # use as flag for has the server created it's files print("Starting server to create settings") try: ret=sp.check_call(["java","-jar",server.data["exe_name"],"nogui"],cwd=server.data["dir"],shell=False,timeout=20) except sp.CalledProcessError as ex: print("Error running server. Java returned status: "+ex.returncode) except sp.TimeoutExpired as ex: print("Error running server. Process didn't complete in time") updateconfig(configfile,{"server-port":str(server.data["port"])}) if eula: updateconfig(eulafile,{"eula":"true"})