def _format(pid, show_all=False): try: p = psutil.Process(pid) except Exception: return "Process {} is inaccessible.".format(pid) if show_all: s = "PID: {}, user: {}, create time: {}, cmd: {}".format( pid, p.username(), _pprint_secs(p.create_time()), p.cmdline()) else: s = "PID: {}, cmd: {}".format(pid, p.cmdline()) return s
def query_process(pid): """Query process info.""" try: p = psutil.Process(pid) (task_id, base_path) = get_task_info(pid) return { "PID": pid, "cmdline": p.cmdline()[2:], "create_time": _pprint_secs(p.create_time()), "cwd": p.cwd(), "task_id": task_id if task_id is not None else "Unknown", "base_path": base_path if base_path is not None else "Unknown", "user": p.username(), } except Exception as e: return { "PID": pid, "message": str(e), }
def run_process_action(action=None): """Run an action with psutil on current process and return a status message.""" action = action or 'status' try: with open(PID_FILE, 'r') as fp: pid = int(fp.read()) p = psutil.Process(pid) if action == 'stop': p.terminate() msg = "pid={}, status=terminated".format(p.pid) else: from psutil import _pprint_secs msg = "pid={}, status={}, created={}".format( p.pid, p.status(), _pprint_secs(p.create_time())) if action == 'stop': os.remove(PID_FILE) except IOError: msg = "No PID file found. Service not running?" except psutil.NoSuchProcess as e: msg = e.msg click.echo(msg)
def run_process_action(action=None): """Run an action with psutil on current process and return a status message.""" action = action or 'status' try: with open(PID_FILE, 'r') as fp: pid = int(fp.read()) p = psutil.Process(pid) if action == 'stop': p.terminate() msg = "pid={}, status=terminated".format(p.pid) else: from psutil import _pprint_secs msg = "pid={}, status={}, created={}".format( p.pid, p.status(), _pprint_secs(p.create_time())) if action == 'stop': os.remove(PID_FILE) except IOError: msg = 'No PID file found. Service not running? Try "netstat -nlp | grep :5000".' except psutil.NoSuchProcess as e: msg = e.msg click.echo(msg)