def __init__(self, pid_file, timeout=0): """ Provide the path to the PID file and the optional timeout value (in seconds) during instantiation. If timeout is set, instantiation will block so many seconds waiting for the PID file to be unlocked by the running instance. Parameters ---------- pid_file : string The PID file to use. timeout : integer, default 0 If > 0, class instantiation will block this many number of seconds waiting for the previous instance to finish """ # Check basics. self.pid_file = pid_file if os.path.isdir(os.path.dirname(pid_file)): self.pdir_exists = True if os.path.isfile(pid_file): self.file_exists = True if self.file_exists and os.access(pid_file, os.W_OK|os.R_OK): self.can_write = True elif self.pdir_exists and os.access(os.path.dirname(pid_file), os.W_OK): self.can_write = True # Look for previous instance. if self.file_exists and self.can_write: old_pid = '' with open(pid_file) as f: old_pid = f.read().strip() if old_pid.isdigit() and int(old_pid) in psutil.get_pid_list(): self.old_pid_exists = True # Wait previous instance to quit. if self.old_pid_exists and timeout: start_time = time.time() while int(old_pid) in psutil.get_pid_list() and time.time() - start_time < timeout: time.sleep(0.5) self.old_pid_exists = True if int(old_pid) in psutil.get_pid_list() else False # Bail if another instance is running. if self.old_pid_exists or not self.can_write: return None # Looks like PID file lock is guaranteed. self._file = open(pid_file, 'w') try: fcntl.flock(self._file.fileno(), fcntl.LOCK_EX) except IOError: # Lost the race, or something happened. self._file.close() return None # We're good. Writing to disk. self._file.write(str(self.pid)) self._file.flush() os.fsync(self._file.fileno()) self.single_instance_success = True atexit.register(self._delete_pid_file_on_exit) # Clean up PID file when this instance exits. return None
def tryPsutil(): pidList = psutil.get_pid_list() print "pidList=", pidList processToTest = "vim" for eachPid in pidList: try: p = psutil.Process(eachPid) processName = p.name() if (processName.lower() == sys.argv[1].lower()): print "Found process" print "processName=", processName processExe = p.exe() print "processExe=", processExe processGetcwd = p.getcwd() print "processGetcwd=", processGetcwd processCmdline = p.cmdline() print "processCmdline=", processCmdline processStatus = p.status() print "processStatus=", processStatus processUsername = p.username() print "processUsername="******"processCreateTime=", processCreateTime print "mem=", p.memory_info() print "cpu=", p.cpu_percent(interval=1) print "cpu=", p.cpu_percent(interval=1) print "Now will terminate this process !" #p.terminate(); #p.wait(timeout=3); #print "psutil.test()=",psutil.test(); except psutil.NoSuchProcess, pid: print "no process found with pid=%s" % (pid)
def wait_until_no_badtest(): bad_test_processes = [] old_psutil = False try: process_list = psutil.pids() except AttributeError: process_list = psutil.get_pid_list() old_psutil = True for p in process_list: try: p_obj = psutil.Process(p) if p_obj is not None: if old_psutil: cmdline_list = psutil.Process(p).cmdline else: cmdline_list = psutil.Process(p).cmdline() if bad_test.path in " ".join(cmdline_list): bad_test_processes.append(p_obj) # psutil.NoSuchProcess happens when the original # process already ended and left the process table except psutil.NoSuchProcess: pass return len(bad_test_processes) == 0
def isMaxOpen(): pids = psutil.get_pid_list() for pid in pids: process = psutil.Process(pid) if process.name == "3dsmax.exe": return True return False
def GetTopProcess(count=3): proc_cpu = dict() proc_mem = dict() process_list = psutil.get_pid_list() for pid in process_list: try: info = psutil.Process(pid) proc_cpu[info.name()] = info.get_cpu_percent() temp_mem = info.memory_percent() for child in info.children(recursive=True): temp_mem += child.memory_percent() proc_mem[info.name()] = temp_mem except: log.info('GetTopProcess except') continue cpu_top = sorted(proc_cpu.iteritems(), key=itemgetter(1), reverse=True)[:count] mem_top = sorted(proc_mem.iteritems(), key=itemgetter(1), reverse=True)[:count] result = '\n*Top ' + str(count) + ' cpu use*\n' for cpu in cpu_top: temp = '%s : %.1f\n' % (cpu[0], cpu[1]) result += temp result += '\n*Top ' + str(count) + ' memory use*\n' for mem in mem_top: temp = '%s : %.1f\n' % (mem[0], mem[1]) result += temp return result
def is_recover_process_running(pid_list=psutil.get_pid_list()): """ Check whether the mysqld process is running with the --wsrep-recover option. Return a boolean. """ return ['/bin/sh', '/usr/bin/mysqld_safe', '--wsrep-recover'] in \ [safe_process(pid, "cmdline") for pid in pid_list]
def connect(self, port, host='', retry=20, interval=1): """ Connect to a Timbl server""" clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) while retry > 0: retry -= 1 try: clientsocket.connect((host, int(port))) clientsocket.recv(1024) # Ignore welcome messages self.socket = clientsocket break except socket.error: time.sleep(interval) continue #Get PID pidlist = psutil.get_pid_list() for i in reversed(pidlist): p = psutil.Process(i) if len(p.cmdline) > 9 and p.cmdline[9] == str(port): self.pid = i break print(self.socket) return True
def test_pid_0(self): # Process(0) is supposed to work on all platforms even if with # some differences p = psutil.Process(0) self.assertTrue(p.name) if os.name == 'posix': self.assertEqual(p.uids.real, 0) self.assertEqual(p.gids.real, 0) self.assertTrue(p.ppid in (0, 1)) #self.assertEqual(p.exe, "") self.assertEqual(p.cmdline, []) try: p.get_num_threads() except psutil.AccessDenied: pass if OSX : #and os.geteuid() != 0: self.assertRaises(psutil.AccessDenied, p.get_memory_info) self.assertRaises(psutil.AccessDenied, p.get_cpu_times) else: p.get_memory_info() # username property if POSIX: self.assertEqual(p.username, 'root') elif WINDOWS: self.assertEqual(p.username, 'NT AUTHORITY\\SYSTEM') else: p.username # PID 0 is supposed to be available on all platforms self.assertTrue(0 in psutil.get_pid_list()) self.assertTrue(psutil.pid_exists(0))
def sys_pids(self, machineid=None, procname=None, procuser=None, includecpid=False): ''' Return the list of process ids for all processes called procname owned by procuser, possibly including the current pid. Does not use list comprehensions to find the pid lists because in between getting the pid list and testing properties of the pids, the processes may terminate and then it is not possible to get the information associated with those pids any more. ''' pidlist = [] pidcurr = psutil.get_pid_list() for pid in pidcurr: try: cmdline = ' '.join(psutil.Process(pid).cmdline()) if procname is None \ or (len(psutil.Process(pid).cmdline()) >= 2 and psutil.Process(pid).cmdline()[0].lower().startswith('python') and procname in cmdline): if procuser is None or psutil.Process( pid).username() == procuser: if machineid is None or machineid in cmdline: pidlist.append(pid) except: pass cpid = os.getpid() if not includecpid and cpid in pidlist: pidlist.remove(cpid) return pidlist
def ps(): ret = [] for p in psutil.get_pid_list(): try: p_info = psutil.Process(p) # If stty if p_info.terminal: terminal = p_info.terminal.replace('/dev/tty', '') else: terminal = '??' # user + system (alias cputime) cpu_time = (p_info.get_cpu_times().user + p_info.get_cpu_times().system) minute = int(cpu_time / 60) cpu_time = str(minute) + ':' + '%.2f' % (cpu_time - minute * 60) ret.append([ p_info.username, p, p_info.get_cpu_percent(), '%.1f' % p_info.get_memory_percent(), p_info.get_memory_info().vms / 1024, # vsz p_info.get_memory_info().rss / 1024, # rss terminal, str(p_info.status), # STAT datetime.fromtimestamp(p_info.create_time).strftime("%I:%M%p"), cpu_time, ' '.join(p_info.cmdline) ]) except (NoSuchProcess, AccessDenied): continue return ret
def test_get_pids(self): # Note: this test might fail if the OS is starting/killing # other processes in the meantime p = get_test_subprocess(["ps", "ax", "-o", "pid"], stdout=subprocess.PIPE) output = p.communicate()[0].strip() if sys.version_info >= (3, ): output = str(output, sys.stdout.encoding) output = output.replace('PID', '') p.wait() pids_ps = [] for pid in output.split('\n'): if pid: pids_ps.append(int(pid.strip())) # remove ps subprocess pid which is supposed to be dead in meantime pids_ps.remove(p.pid) pids_psutil = psutil.get_pid_list() pids_ps.sort() pids_psutil.sort() # on OSX ps doesn't show pid 0 if OSX and 0 not in pids_ps: pids_ps.insert(0, 0) if pids_ps != pids_psutil: difference = [x for x in pids_psutil if x not in pids_ps] + \ [x for x in pids_ps if x not in pids_psutil] self.fail("difference: " + str(difference))
def getProcesses(self): returnValue = {} try: pids = psutil.get_pid_list() for pid in pids: p = psutil.Process(pid) data = {} data["name"] = p.name data["cmdline"] = p.cmdline data["uid"] = p.uid data["gid"] = p.gid data["username"] = p.username cputimes = p.get_cpu_times() data["cpu_user_time"] = cputimes[0] data["cpu_system_time"] = cputimes[1] data["cpu_percent"] = p.get_cpu_percent() meminfo = p.get_memory_info() data["memory_rss"] = meminfo[0] data["memory_vms"] = meminfo[1] data["memory_percent"] = p.get_memory_percent() returnValue[p.pid] = data except Exception, e: returnValue = {} returnValue["error"] = "Unable to get process data because " + str(e) returnValue["errorcode"] = 1 syslog.syslog(syslog.LOG_WARNING, returnValue["error"]) return returnValue
def GetPIDs(): """ GetPIDs() Get a list of active processes PIDs on current host. """ import psutil return psutil.get_pid_list()
def Start(self): if self.__quit: self.__DumpData() return threading.Timer(0.1, self.Start).start() for pid in psutil.get_pid_list(): p = None try: p = psutil.Process(pid) except NoSuchProcess: continue cpu = p.get_cpu_times() user_cpu = cpu[0] system_cpu = cpu[1] name = p.name if pid not in self.__process: print 'found new process %s (%d)' % (name, pid) self.__process[pid] = (name, 0.0, 0.0) if not self.__WorthMonitor(name): continue process = self.__process[pid] assert process[0] == name assert process[1] <= user_cpu assert process[2] <= system_cpu self.__process[pid] = (name, user_cpu, system_cpu)
def kill_ghost_brokers(broker_endpoint=DEFAULT_FRONTEND, timeout=1.): """Kills ghost brokers. Return a pid, pids tuple. The first pid is the running broker and the second is a list of pids that where killed. """ # checking if there's a working broker working_broker = verify_broker(broker_endpoint, timeout) if working_broker is not None: working_broker = int(working_broker) # listing running brokers and killing the ghost ones killed = [] import psutil for pid in psutil.get_pid_list(): if pid in (os.getpid(), os.getppid()): continue p = psutil.Process(pid) try: cmd = ' '.join(p.cmdline) except psutil.error.AccessDenied: continue cmd = cmd.replace('-', '.') if 'powerhose.broker' not in cmd or pid == working_broker: continue killed.append(pid) p.terminate() return working_broker, killed
def which_apps_are_running(apps): ''' Check the process list to see if any of the specified apps are running. It returns a tuple of running apps. ''' running = [] for pid in psutil.get_pid_list(): try: p = psutil.Process(pid) except Exception as e: print(e) continue for app in apps: if app == p.name: running.append(app) print('found: ' + p.name) else: r = re.compile('.*' + app + '.*', re.IGNORECASE) try: for arg in p.cmdline: m = r.match(arg) if m and (p.name == 'python' or p.name == 'java'): running.append(app) break except: pass return tuple(running)
def setDisplayToLocalX(): """ set DISPLAY env var in this process to the id of the X process on localhost I might need something like xauth merge /run/gdm/auth-for-gdm-xxxxxx/database too and this isn't automated yet """ for pid in psutil.get_pid_list(): try: proc = psutil.Process(pid) if proc.exe not in ['/usr/bin/Xorg', '/usr/bin/X', '/usr/bin/X11/X', '/usr/lib/xorg/Xorg']: continue except (psutil.error.AccessDenied, psutil.error.NoSuchProcess): continue argIter = iter(proc.cmdline) while True: arg = argIter.next() if arg in ['-background']: argIter.next() continue if arg in ['-nolisten']: continue if arg.startswith(':'): display = arg break if arg == 'tcp': display = ':0.0' break assert display.startswith(':'), display os.environ['DISPLAY'] = display os.environ['XAUTHORITY'] = os.path.expanduser('~/.Xauthority') break else: raise ValueError("didn't find an Xorg process")
def _killGroup(): global _terminating _terminating = True mypid = getpid() for pid in get_pid_list(): if getpgid(pid) == mypid and pid != mypid: kill(pid, SIGTERM)
def top(num_processes=5, interval=3): ''' Return a list of top CPU consuming processes during the interval. num_processes = return the top N CPU consuming processes interval = the number of seconds to sample CPU usage over ''' result = [] start_usage = {} for pid in psutil.get_pid_list(): p = psutil.Process(pid) user, sys = p.get_cpu_times() start_usage[p] = user + sys time.sleep(interval) usage = set() for p, start in start_usage.iteritems(): user, sys = p.get_cpu_times() now = user + sys diff = now - start usage.add((diff, p)) for i, (diff, p) in enumerate(reversed(sorted(usage))): if num_processes and i >= num_processes: break if len(p.cmdline) == 0: cmdline = [p.name] else: cmdline = p.cmdline info = {'cmd': cmdline, 'pid': p.pid, 'create_time': p.create_time} for k, v in p.get_cpu_times()._asdict().iteritems(): info['cpu.' + k] = v for k, v in p.get_memory_info()._asdict().iteritems(): info['mem.' + k] = v result.append(info) return result
def status(): """ Print a word describing the status of mysqld or the initiator script. """ # Get a list of all currently running processes. pid_list = psutil.get_pid_list() # Check if a process looks like a bootstrap process. if is_boostrap_process_running(pid_list): print("bootstrapping") # Check if a process looks like a recover process. elif is_recover_process_running(): # It doesn't really mean that the local machine is locked, # but that another node probably has the lock. print("locked") # Check if a process looks like a galera_init process. elif is_mysqld_process_running(pid_list): print("running") # Check if a lock file exists elif lock_file_exists(): print("locked") # Check if a process looks like a galera_init process. elif is_galera_init_process_running(pid_list): print("initiating") # If none of the above is true, the status is simply stopped. else: print("stopped") sys.exit(0)
def get_home(): for i in filter( lambda x: psutil.Process(x).name.startswith("eucalyptus-cl"), psutil.get_pid_list()): home = get_home_cmdline(psutil.Process(i).cmdline) if home: return home print "ERROR: No running eucalyptus-cloud process found."
def updateScreen(self): dt = datetime.datetime.now() with self.context(): self._text[1] = dt.strftime('%A, %b %d, %Y').center(27) self._text[2] = dt.strftime('%I:%M:%S %p').center(27) self._text[3] = ('CPU:%4.1f%% RAM:%6.2f%%' % (psutil.cpu_percent(), (psutil.used_phymem()-psutil.cached_phymem())/psutil.TOTAL_PHYMEM*100)).center(27) self._text[4] = ('%i processes' % len(psutil.get_pid_list())).center(27)
def validateProccess(self, method, procc): if method == "ID": try: proc = int(procc) PIDS = psutil.get_pid_list() x = 0 while x < (len(PIDS) + 1): if x != len(PIDS): if PIDS[x] == proc: Processes.TerminateProccess(self, method, proc) #print PIDS[x] break else: pass else: print "Invalid Process or Process Method." x += 1 except ValueError: print "Invalid Process or Process Method." else: Processes.TerminateProccess(self, method, procc)
def test_zombies(self): # test that NPS is raised by the 2nd implementation in case a # process no longer exists ZOMBIE_PID = max(psutil.get_pid_list()) + 5000 for name, _ in self.fun_names: meth = wrap_exceptions(getattr(_psutil_mswindows, name)) self.assertRaises(psutil.NoSuchProcess, meth, ZOMBIE_PID)
def xpdf_server_file_dict(): # Make dictionary of the type { xpdf_servername : [ file, xpdf_pid ] }, # to test if the server host file use: # basename(xpdf_server_file_dict().get(server, ['_no_file_'])[0]) == basename(file) # this dictionary always contains the full path (Linux). # TODO: this is not working as I want to: # when the xpdf was opened first without a file it is not visible in the command line # I can use 'xpdf -remote <server> -exec "run('echo %f')"' # where get_filename is a simple program which returns the filename. # Then if the file matches I can just reload, if not I can use: # xpdf -remote <server> -exec "openFile(file)" ps_list = psutil.get_pid_list() server_file_dict = {} for pr in ps_list: try: p = psutil.Process(pr) if psutil.version_info[0] >= 2: name = p.name() cmdline = p.cmdline() else: name = p.name cmdline = p.cmdline if name == 'xpdf': try: ind = cmdline.index('-remote') except: ind = 0 if ind != 0 and len(cmdline) >= 1: server_file_dict[cmdline[ind + 1]] = [ cmdline[len(cmdline) - 1], pr ] except (NoSuchProcess, AccessDenied): pass return server_file_dict
def update_list(self): if bool(self.cfg.get_desktop()): for de in self.cfg.get_desktop(): ps = psutil.Process(get_pid_by_name(de)) pl = ps.children() for p in pl: if p.uids().real == self.userid: if p.name not in self.cfg.get_process(): short_info = '%s / %s / %s' % (p.pid, p.name(), p.status()) self.ps_list.item_append(label=short_info, callback=self.update_info, p=p) else: pl = psutil.get_pid_list() for p in pl: p = psutil.Process(p) if p.uids().real == self.userid: if p.name() not in self.cfg.get_process(): short_info = '%s / %s / %s' % (p.pid, p.name(), p.status()) self.ps_list.item_append(label=short_info, callback=self.update_info, p=p)
def tryPsutil(): pidList = psutil.get_pid_list() print "pidList=", pidList processToTest = "QQ.exe" # processToTest = "YodaoDict.exe" for eachPid in pidList: try: eachProcess = psutil.Process(eachPid) # print "eachProcess=",eachProcess; processName = eachProcess.name if(processName.lower() == processToTest.lower()): print "Found process" print "processName=", processName processExe = eachProcess.exe print "processExe=", processExe processGetcwd = eachProcess.getcwd() print "processGetcwd=", processGetcwd processCmdline = eachProcess.cmdline print "processCmdline=", processCmdline processStatus = eachProcess.status print "processStatus=", processStatus processUsername = eachProcess.username print "processUsername="******"processCreateTime=", processCreateTime print "Now will terminate this process !" eachProcess.terminate() eachProcess.wait(timeout=3) print "psutil.test()=", psutil.test() except psutil.NoSuchProcess, pid: print "no process found with pid=%s" % (pid)
def test_get_pids(self): # Note: this test might fail if the OS is starting/killing # other processes in the meantime p = get_test_subprocess(["ps", "ax", "-o", "pid"], stdout=subprocess.PIPE) output = p.communicate()[0].strip() if sys.version_info >= (3,): output = str(output, sys.stdout.encoding) output = output.replace("PID", "") p.wait() pids_ps = [] for pid in output.split("\n"): if pid: pids_ps.append(int(pid.strip())) # remove ps subprocess pid which is supposed to be dead in meantime pids_ps.remove(p.pid) pids_psutil = psutil.get_pid_list() pids_ps.sort() pids_psutil.sort() # on OSX ps doesn't show pid 0 if OSX and 0 not in pids_ps: pids_ps.insert(0, 0) if pids_ps != pids_psutil: difference = [x for x in pids_psutil if x not in pids_ps] + [x for x in pids_ps if x not in pids_psutil] self.fail("difference: " + str(difference))
def find_program(pidfile, verifier): '''Return the psutil.Process if process is running, else None''' if pidfile: try: pid = int(open(pidfile).read()) p = psutil.Process(pid) pcmd = ' '.join(p.cmdline()) if verifier not in pcmd: raise VerifierFailed(verifier + '::' + pcmd) else: return p except (IOError, psutil.NoSuchProcess, VerifierFailed): return None else: # directly use verifier matches = [] for pid in psutil.get_pid_list(): try: p = psutil.Process(pid) except psutil.error.NoSuchPress: pass pcmd = ' '.join(p.cmdline()) if verifier in pcmd: matches.append(p) if len(matches) == 2: raise MultipleProcessesFound('{}: Skipping'.format(verifier)) elif len(matches) == 0: return None else: return matches[0]
def classifyingUsers(): d, pids = unpickle(SHARED_DATA_LOCK) or {}, set(psutil.get_pid_list()) print "classifying users (by video):\n %s" % ( "none" if not d else "\n ".join( "%d: %s (pid %d%s)" % (k, u, pid, "" if pid in pids else " terminated") for k, (u, pid) in d.iteritems()))
def _no_test_in_process_table(self): """ Make sure the test will be really gone from the process table. """ test_processes = [] old_psutil = False try: process_list = psutil.pids() except AttributeError: process_list = psutil.get_pid_list() old_psutil = True for p in process_list: try: p_obj = psutil.Process(p) if p_obj is not None: if old_psutil: cmdline_list = psutil.Process(p).cmdline else: try: cmdline_list = psutil.Process(p).cmdline() except psutil.AccessDenied: cmdline_list = [] if self.test_module in " ".join(cmdline_list): test_processes.append(p_obj) # psutil.NoSuchProcess happens when the original # process already ended and left the process table except psutil.NoSuchProcess: pass return len(test_processes) == 0
def test_get_pids(self): # Note: this test might fail if the OS is starting/killing # other processes in the meantime p = get_test_subprocess(["ps", "ax", "-o", "pid"], stdout=subprocess.PIPE) output = p.communicate()[0].strip() if sys.version_info >= (3, ): output = str(output, sys.stdout.encoding) output = output.replace('PID', '') p.wait() pids_ps = [] for pid in output.split('\n'): if pid: pids_ps.append(int(pid.strip())) # remove ps subprocess pid which is supposed to be dead in meantime pids_ps.remove(p.pid) # not all systems include pid 0 in their list but psutil does so # we force it if 0 not in pids_ps: pids_ps.append(0) pids_psutil = psutil.get_pid_list() pids_ps.sort() pids_psutil.sort() if pids_ps != pids_psutil: difference = filter(lambda x:x not in pids_ps, pids_psutil) + \ filter(lambda x:x not in pids_psutil, pids_ps) self.fail("difference: " + str(difference))
def CheckIfRunning(): Message("Checking if this task is already running...") try: processes=psutil.get_pid_list() except: processes=psutil.pids() for process in processes: if process != os.getpid(): try: ps=psutil.Process(process) try: cmdline=ps.cmdline try: len_cmdline=len(ps.cmdline) except: len_cmdline=len(ps.cmdline()) cmdline=ps.cmdline() if len_cmdline>1: if cmdline[1] == sys.argv[0]: Message("Already running process %s '%s'" % (process,cmdline)) sys.exit(1) except psutil.AccessDenied: Message("I can't read information for process %s" % process) except psutil.NoSuchProcess: Nothing=0
def fetch_busy_processes(): """Query all PIDs for memory and CPU usage.""" now = int(time.time()) busy_pids = [] # for p in psutil.get_pid_list(): try: pid = psutil.Process(p) p_cpu = pid.get_cpu_percent() p_mem = pid.get_memory_percent() p_name = pid.name # logging.debug( "Name: %s, CPU: %s, MEM: %s" %(p_name, p_cpu, p_mem)) if p_cpu >= cpu_threshold or p_mem >= mem_threshold: busy_pid = { 'name': p_name, 'pid': p, 'cpu': p_cpu, 'mem': p_mem } busy_pids.append(busy_pid) except psutil.NoSuchProcess: pass except psutil.AccessDenied: loggging.error("Access denied when trying to query PID %d" % p) except psutil.TimeoutExpired: logging.error("Timed out when trying to query PID %d" % p) return busy_pids
def getAllPIDs(): pids = [] for pid in psutil.get_pid_list(): proc = psutil.Process(pid) if(proc.name=="World of Warcraft"): pids.append(pid) return pids
def __build_impala_process_lists(self): """ Gets all the running Impala procs (with start arguments) on the machine. Note: This currently only works for the local case. To support running in a cluster environment this would need to enumerate each machine in the cluster. """ impalads = list() statestored = list() catalogd = None for pid in psutil.get_pid_list(): try: process = psutil.Process(pid) try: if process.username != getuser(): continue except KeyError, e: if "uid not found" in str(e): continue raise if process.name == 'impalad' and len(process.cmdline) >= 1: impalads.append(ImpaladProcess(process.cmdline)) elif process.name == 'statestored' and len( process.cmdline) >= 1: statestored.append(StateStoreProcess(process.cmdline)) elif process.name == 'catalogd' and len(process.cmdline) >= 1: catalogd = CatalogdProcess(process.cmdline) except psutil.NoSuchProcess, e: # A process from get_pid_list() no longer exists, continue. LOG.info(e) continue
def start_monitor(self): temporary_monitoring = MonitoringTool() #check if the process is alive == None, instead zero value == proc is over while self.proc.pid in psutil.get_pid_list(): conn = sqlite3.connect(self.path_database) cursor = conn.cursor() cursor.execute("create table if not exists data (cpu real, cpu_time real, memory real, rss real," \ "date text, time text, reported integer NULL DEFAULT 0)") try: res_list = temporary_monitoring.get_cpu_and_memory_usage(self.proc) date_catched = "-".join([str(res_list[4].year), str(res_list[4].month), str(res_list[4].day)]) time_catched = ":".join([str(res_list[4].hour), str(res_list[4].minute), str(res_list[4].second)]) res_list[4] = date_catched res_list.append(time_catched) cursor.execute("insert into data(cpu, cpu_time, memory, rss, date, time) values (?,?,?,?,?,?)" , res_list) conn.commit() cursor.close() conn.close() time.sleep(self.update_time) except IOError: if log_file: logging.info("ERROR : process with pid : %s watched by slap monitor exited too quickly at %s" % (self.proc.pid, time.strftime("%Y-%m-%d at %H:%m"))) sys.exit(1) if log_file: logging.info("EXIT 0: Process terminated normally!") sys.exit(0)
def processs(): lprocess = get_pid_list() lAllprocess = [] dprocess = {} for pid in lprocess: pName = Process(pid).name() pMemoryPercent = Process(pid).memory_percent() pMemoryInfo = Process(pid).memory_info() pCpuAfinity = Process(pid).cpu_affinity() pGids = Process(pid).gids() pUids = Process(pid).uids() pUsername = Process(pid).username() try: pCwd = Process(pid).cwd() except: pCwd = '' dprocess = { 'pid': pid, 'name': pName, 'Memory%': pMemoryPercent, 'Cpu Afinity': pCpuAfinity, 'CWD': pCwd, 'Memory info': pMemoryInfo, 'Gids': pGids, 'Uids': pUids, 'Username': pUsername } lAllprocess.append(dprocess) return jsonify(process=lAllprocess)
def xpdf_server_file_dict(): # Make dictionary of the type { xpdf_servername : [ file, xpdf_pid ] }, # to test if the server host file use: # basename(xpdf_server_file_dict().get(server, ['_no_file_'])[0]) == basename(file) # this dictionary always contains the full path (Linux). # TODO: this is not working as I want to: # when the xpdf was opened first without a file it is not visible in the command line # I can use 'xpdf -remote <server> -exec "run('echo %f')"' # where get_filename is a simple program which returns the filename. # Then if the file matches I can just reload, if not I can use: # xpdf -remote <server> -exec "openFile(file)" ps_list=psutil.get_pid_list() server_file_dict={} for pr in ps_list: try: name=psutil.Process(pr).name cmdline=psutil.Process(pr).cmdline if name == 'xpdf': try: ind=cmdline.index('-remote') except: ind=0 if ind != 0 and len(cmdline) >= 1: server_file_dict[cmdline[ind+1]]=[cmdline[len(cmdline)-1], pr] except psutil.error.NoSuchProcess: pass except psutil.error.AccessDenied: pass return server_file_dict
def check_processes(): # Conda should still work if psutil is not installed (it should not be a # hard dependency) try: import psutil # Old versions of psutil don't have this error, causing the below code to # fail. psutil._error.AccessDenied except: return True ok = True curpid = os.getpid() for n in psutil.get_pid_list(): if n == curpid: # The Python that conda is running is OK continue try: p = psutil.Process(n) except psutil._error.NoSuchProcess: continue try: if os.path.realpath(p.exe).startswith(os.path.realpath(root_dir)): processcmd = ' '.join(p.cmdline) print "WARNING: the process %s (%d) is running" % (processcmd, n) ok = False except (psutil._error.AccessDenied, WindowsError): pass if not ok: print("WARNING: Continuing installation while the above processes are " "running is not recommended.\n" "Close all Anaconda programs before installing or updating things with conda.") return ok
def test_get_pids(self): # Note: this test might fail if the OS is starting/killing # other processes in the meantime p = get_test_subprocess(["ps", "ax", "-o", "pid"], stdout=subprocess.PIPE) output = p.communicate()[0].strip() if sys.version_info >= (3,): output = str(output, sys.stdout.encoding) output = output.replace('PID', '') p.wait() pids_ps = [] for pid in output.split('\n'): if pid: pids_ps.append(int(pid.strip())) # remove ps subprocess pid which is supposed to be dead in meantime pids_ps.remove(p.pid) # not all systems include pid 0 in their list but psutil does so # we force it if 0 not in pids_ps: pids_ps.append(0) pids_psutil = psutil.get_pid_list() pids_ps.sort() pids_psutil.sort() if pids_ps != pids_psutil: difference = filter(lambda x:x not in pids_ps, pids_psutil) + \ filter(lambda x:x not in pids_psutil, pids_ps) self.fail("difference: " + str(difference))
def test_get_pids(self): # Note: this test might fail if the OS is starting/killing # other processes in the meantime if SUNOS: cmd = ["ps", "ax"] else: cmd = ["ps", "ax", "-o", "pid"] p = get_test_subprocess(cmd, stdout=subprocess.PIPE) output = p.communicate()[0].strip() if PY3: output = str(output, sys.stdout.encoding) pids_ps = [] for line in output.split('\n')[1:]: if line: pid = int(line.split()[0].strip()) pids_ps.append(pid) # remove ps subprocess pid which is supposed to be dead in meantime pids_ps.remove(p.pid) pids_psutil = psutil.get_pid_list() pids_ps.sort() pids_psutil.sort() # on OSX ps doesn't show pid 0 if OSX and 0 not in pids_ps: pids_ps.insert(0, 0) if pids_ps != pids_psutil: difference = [x for x in pids_psutil if x not in pids_ps] + \ [x for x in pids_ps if x not in pids_psutil] self.fail("difference: " + str(difference))
def __build_impala_process_lists(self): """ Gets all the running Impala procs (with start arguments) on the machine. Note: This currently only works for the local case. To support running in a cluster environment this would need to enumerate each machine in the cluster. """ impalads = list() statestored = list() catalogd = None for pid in psutil.get_pid_list(): try: process = psutil.Process(pid) try: if process.username != getuser(): continue except KeyError, e: if "uid not found" in str(e): continue raise if process.name == 'impalad' and len(process.cmdline) >= 1: impalads.append(ImpaladProcess(process.cmdline)) elif process.name == 'statestored' and len(process.cmdline) >= 1: statestored.append(StateStoreProcess(process.cmdline)) elif process.name == 'catalogd' and len(process.cmdline) >= 1: catalogd = CatalogdProcess(process.cmdline) except psutil.NoSuchProcess, e: # A process from get_pid_list() no longer exists, continue. LOG.info(e) continue
def pull(self): for pid in psutil.get_pid_list(): process_uri = self.uri_ref(str(pid), 'processes') try: p = psutil.Process(pid) user_uri = self.uri_ref(p.username(), 'users') command_uri = self.uri_ref(str(pid), 'commands') self.graph.add((process_uri, RDF.type, T['Process'])) self.graph.add((process_uri, T['Name'], Literal(p.name()))) self.graph.add((process_uri, T['ProcessID'], Literal(int(pid)))) self.graph.add((process_uri, T['Command'], Literal(' '.join(p.cmdline())))) self.graph.add((process_uri, T['Status'], Literal(p.status()))) self.graph.add((process_uri, T['Cwd'], Literal(p.cwd()))) self.graph.add((process_uri, T['Terminal'], Literal(p.cwd()))) self.graph.add((process_uri, T['CPUPercent'], Literal(float(p.cpu_percent())))) self.graph.add((process_uri, T['RAMPercent'], Literal(float(p.memory_percent())))) self.graph.add((process_uri, T['launchedBy'], user_uri)) self.graph.add((process_uri, T['executedBy'], command_uri)) self.graph.add((user_uri, RDF.type, T['User'])) self.graph.add((user_uri, T['Name'], Literal(p.username()))) self.graph.add((user_uri, T['launches'], process_uri)) self.graph.add((command_uri, RDF.type, T['Command'])) self.graph.add((command_uri, T['Binary'], Literal(p.exe()))) self.graph.add((command_uri, T['Argument'], Literal(' '.join(p.cmdline()[1:])))) self.graph.add((command_uri, T['executes'], process_uri)) except psutil.AccessDenied: logger.critical("AccessDenied: Are you root ?") except psutil.NoSuchProcess: logger.warning("NoSuchProcess: %s"%pid) self.graph.add((process_uri, RDF.type, T['Process'])) self.graph.add((process_uri, T['Status'], Literal('zombie')))
def classifyingUsers(): d, pids = unpickle(SHARED_DATA_LOCK) or {}, set(psutil.get_pid_list()) print "classifying users (by video):\n %s" %( "none" if not d else "\n ".join("%d: %s (pid %d%s)" %(k, u, pid, "" if pid in pids else " terminated") for k, (u, pid) in d.iteritems()))
def test_get_pids(self): # Note: this test might fail if the OS is starting/killing # other processes in the meantime if SUNOS: cmd = ["ps", "ax"] else: cmd = ["ps", "ax", "-o", "pid"] p = get_test_subprocess(cmd, stdout=subprocess.PIPE) output = p.communicate()[0].strip() if PY3: output = str(output, sys.stdout.encoding) pids_ps = [] for line in output.split("\n")[1:]: if line: pid = int(line.split()[0].strip()) pids_ps.append(pid) # remove ps subprocess pid which is supposed to be dead in meantime pids_ps.remove(p.pid) pids_psutil = psutil.get_pid_list() pids_ps.sort() pids_psutil.sort() # on OSX ps doesn't show pid 0 if OSX and 0 not in pids_ps: pids_ps.insert(0, 0) if pids_ps != pids_psutil: difference = [x for x in pids_psutil if x not in pids_ps] + [x for x in pids_ps if x not in pids_psutil] self.fail("difference: " + str(difference))
def test_pid_0(self): # Process(0) is supposed to work on all platforms even if with # some differences p = psutil.Process(0) self.assertTrue(p.name) if os.name == 'posix': self.assertEqual(p.uids.real, 0) self.assertEqual(p.gids.real, 0) self.assertTrue(p.ppid in (0, 1)) #self.assertEqual(p.exe, "") self.assertEqual(p.cmdline, []) try: p.get_num_threads() except psutil.AccessDenied: pass if OSX: #and os.geteuid() != 0: self.assertRaises(psutil.AccessDenied, p.get_memory_info) self.assertRaises(psutil.AccessDenied, p.get_cpu_times) else: p.get_memory_info() # username property if POSIX: self.assertEqual(p.username, 'root') elif WINDOWS: self.assertEqual(p.username, 'NT AUTHORITY\\SYSTEM') else: p.username # PID 0 is supposed to be available on all platforms self.assertTrue(0 in psutil.get_pid_list()) self.assertTrue(psutil.pid_exists(0))
def main(): print "=======================START=======================" for proc in psutil.get_pid_list(): try: run(proc) print "*" * 50 except: pass
def listProcesses(): """Returns a list of PID's""" if HAS_PROC_DIR: try: return [int(p) for p in os.listdir(PROC_DIR) if p.isdigit()] except: pass #rather be slow than blow up return psutil.get_pid_list()
def process(self, type): list = psutil.get_pid_list() if type == 'total_process': return '{0}'.format(len(list)) if type == 'processname': for pid in list: p = psutil.Process(pid) return p.name
def check_online(self): if self.pid in psutil.get_pid_list(): if not self.online: self.online = True self.save() return True self.set_offline() return False