def test_test(self): # test for psutil.test() function stdout = sys.stdout sys.stdout = DEVNULL try: psutil.test() finally: sys.stdout = stdout
def main(): print(psutil.cpu_count()) print(psutil.cpu_count(logical=False)) print(psutil.cpu_times()) for x in range(10): print(psutil.cpu_percent(interval=1, percpu=True)) print(psutil.pids()) psutil.test()
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 kill_process(process_name): ''' 杀死进程 :param process_name: 进程名 :return:None ''' pid_list = psutil.pids() for each_pid in pid_list: try: each_pro = psutil.Process(each_pid) if each_pro.name().lower() == process_name.lower(): print("found process") print("process_name=%s" % each_pro.name()) print('process_exe=%s' % each_pro.exe()) print('process_cwd=%s' % each_pro.cwd()) print('process_cmdline=%s' % each_pro.cmdline()) print('process_status=%s' % each_pro.status()) print('process_username=%s' % each_pro.username()) print('process_createtime=%s' % each_pro.create_time()) print('now will kill this process') each_pro.terminate() each_pro.wait(timeout=3) print('psutil.test():\n%s' % psutil.test()) except psutil.NoSuchProcess, pid: print("no process found with pid=%s" % pid)
def terminal_process_by_name(process_name): pidList = psutil.pids(); found_target_process=False for eachPid in pidList: try: eachProcess = psutil.Process(eachPid); processName = eachProcess.name; if (str(processName).lower().find( str(process_name).lower())>0): found_target_process=True print("Found process"); print("processName=", processName); processExe = eachProcess.exe; print("processExe=", processExe); processGetcwd = eachProcess.cwd(); 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 !"); print("终止目标进程!") eachProcess.terminate(); eachProcess.wait(timeout=3); print("最新进程列表!") print("psutil.test()=", psutil.test()); except psutil.NoSuchProcess as pid: print("no process found with pid="+pid); if not found_target_process: print("进程:"+process_name+" 没有发现")
def test_com(): print(psutil.cpu_count()) # 逻辑CPU数 print(psutil.cpu_count(logical=False)) # 物理CPU数 print(psutil.cpu_times()) # 统计CPU的用户/系统/空闲时间 print(psutil.cpu_percent(interval=1, percpu=True)) # CPU使用率,每秒刷新一次 print(psutil.test()) # 模拟ps命令
def data(): while True: cpu_p = psutil.cpu_percent(interval=1, percpu=True) mem_t = psutil.virtual_memory().total / 1024 / 1024 / 1024 mem_t = round(mem_t, 2) mem_u = psutil.virtual_memory().used / 1024 / 1024 / 1024 mem_u = round(mem_u, 2) mem_p = psutil.virtual_memory().percent t3.set(cpu_p) tt = str(mem_u) + "GB / " + str(mem_t) + "GB(" + str(mem_p) + "%)" t4.set(tt) net_in = psutil.net_io_counters().bytes_recv / 1024 / 1024 net_in = round(net_in, 2) net_out = psutil.net_io_counters().bytes_sent / 1024 / 1024 net_out = round(net_out, 2) net = str(net_in) + "MB / " + str(net_out) + "MB" t5.set(net) disk_u = psutil.disk_usage("/").percent disk_u = str(disk_u) + "%" t6.set(disk_u) pp = list(psutil.test()) t7.set(pp) sleep(0.5)
def tryPsutil(): pidList = psutil.get_pid_list() print "pidList=", pidList processToTest = "services.exe" # processToTest = "YodaoDict.exe"; for eachPid in pidList: try: eachProcess = psutil.Process(eachPid) print "eachProcess=", eachProcess processName = eachProcess._name print "-----------------------------------------------------------processName=", processName if processName: 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 tryPsutil(): pidList = psutil.get_pid_list(); print "pidList=",pidList; processToTest = "services.exe"; # processToTest = "YodaoDict.exe"; for eachPid in pidList: try: eachProcess = psutil.Process(eachPid); print "eachProcess=",eachProcess; processName = eachProcess._name; print "-----------------------------------------------------------processName=",processName; if processName: 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 taska(): print 'THIS IS THE LIST OF ALL PROCESS RUNNING' print psutil.test()
def imitate_ps(): #psutil还提供了一个test()函数,可以模拟出ps命令的效果: psutil.test()
''' 若提示:No module named 'psutil' 且:pip install psutil无效 则尝试:sudo apt-get install python3-pip 再执行:pip3 install psutil ''' print(psutil.cpu_count()) #CPU逻辑数量 print(psutil.cpu_count(logical=False)) #CPU物理核心 print(psutil.cpu_times()) #统计cpu的用户、系统、空闲时间 print(psutil.virtual_memory()) #获取内存信息 print(psutil.swap_memory()) #获取swap信息 print(psutil.disk_partitions()) #获取磁盘分区信息 print(psutil.disk_usage('/')) #获取磁盘使用情况 print(psutil.disk_io_counters()) #获取磁盘IO print(psutil.net_io_counters()) #获取网络读写字节/包的个数 print(psutil.net_if_addrs()) #获取网络接口信息 print(psutil.net_if_stats()) #获取网络接口状态 #print(psutil.net_connections()) #获取当前网络连接信息,需要root权限 #p = psutil.Process(3776) #获取指定进程ID=3776,其实就是当前Python交互环境 #print(p.name()) #进程名称 for x in range(10): #CPU使用率,每秒刷新一次,累计10次 print(psutil.cpu_percent(interval=1, percpu=True)) print(psutil.test()) #test()函数,可以模拟出ps命令的效果
pid = find_pid(args.process_name) if pid >= 0: p = psutil.Process(pid) cnt = 0 while True: if args.verbose: print( ( "%s, %.6f, %.2f, %.2f" % ( time.strftime("%a %H:%M:%S"), p.memory_percent(), p.cpu_percent(interval=args.wait_time_s), psutil.virtual_memory()[2], ) ), flush=True, ) else: print( ( "%.6f, %.2f, %.2f" % (p.memory_percent(), p.cpu_percent(interval=args.wait_time_s), psutil.virtual_memory()[2]) ), flush=True, ) cnt += 1 if cnt % 10 == 0: print(psutil.test(), flush=True)
def process_fun(): psutil.pids() psutil.test()
print("# 获取网络读写字节/包的个数", psutil.net_io_counters()) print("获取网络接口信息", psutil.net_if_addrs()) print("获取网络接口状态", psutil.net_if_stats()) #要获取当前网络连接信息,使用net_connections(): print("获取当前网络连接信息", psutil.net_connections()) #获取进程信息 print("所有进程ID", psutil.pids()) #p = psutil.Process(3776) # 获取指定进程ID=3776,其实就是当前Python交互环境 # print("进程名称",p.name()) # print("进程exe路径",p.exe()) # print("进程工作目录",p.cwd()) # print("进程启动的命令行",p.cmdline()) # print("父进程",p.parent()) # print("子进程列表",p.children()) # print("进程状态",p.status()) # print("进程用户名",p.username()) # print("进程创建时间",p.create_time()) # print("进程终端",p.terminal()) # print("进程使用的CPU时间",p.cpu_times()) # print("进程使用的内存",p.memory_info()) # print("进程打开的文件",p.open_files()) # print("进程相关网络连接",p.connections()) # print("进程的线程数量",p.num_threads()) # print("所有线程信息",p.threads()) # print("进程环境变量",p.environ()) # print("结束进程",p.terminate()) print("模拟出ps命令的效果:", psutil.test())
def main(argv=None): print "Main Function..." print "Current process ID: {}".format(os.getpid()) f = os.open('/tmp/cpu_monitor.pid', os.O_RDWR|os.O_CREAT) os.write(f, str(os.getpid())) os.close(f) #################################### CPU print "#### CPU ####" print "\npsutil.cpu_times()" print psutil.cpu_times() print psutil.cpu_times() wait_time=1 # seconds loops=1 print "\npsutil.cpu_percent(interval={time})".format(time=wait_time) for x in range(loops): print psutil.cpu_percent(interval=wait_time) print "\npsutil.cpu_percent(interval={time}, percpu=True)".format(time=wait_time) for x in range(loops): print psutil.cpu_percent(interval=wait_time, percpu=True) print "\npsutil.cpu_times_percent(interval={time}, percpu=True)".format(time=wait_time) for x in range(loops): print psutil.cpu_times_percent(interval=wait_time, percpu=False) print "\npsutil.cpu_count() & psutil.cpu_count(logical=False)" print psutil.cpu_count() print psutil.cpu_count(logical=False) print "#### CPU/END ####" #################################### MEMORY print "#### MEMORY ####" print "\npsutil.virtual_memory()" print psutil.virtual_memory() print "\npsutil.swap_memory()" print psutil.swap_memory() print "#### MEMORY/END ####" #################################### DISK print "#### DISK ####" print "\npsutil.disk_partitions()" print psutil.disk_partitions() print "\npsutil.disk_usage('/'))" print psutil.disk_usage('/') print "\npsutil.disk_io_counters(perdisk=False) & True" print psutil.disk_io_counters(perdisk=False) print psutil.disk_io_counters(perdisk=True) print "#### DISK/END ####" #################################### NETWORK print "#### NETWORK ####" print "\npsutil.net_io_counters(pernic=True)" print psutil.net_io_counters(pernic=True) print "\npsutil.net_connections()" print psutil.net_connections() #print "\npsutil.net_if_addrs()" #print psutil.net_if_addrs() # available but still not released #print "\npsutil.net_if_stats()" #print psutil.net_if_stats() print "#### NETWORK/END ####" #################################### MISC print "#### MISC ####" print "\npsutil.users()" print psutil.users() print "\npsutil.boot_time()" print psutil.boot_time() print "#### MISC/END ####" #################################### PROCESS MANAGEMENT print "#### PROCESS MANAGEMENT ####" print "\npsutil.pids()" print psutil.pids() # lookup for process called stacksync proc_watchdog = 0 for x in psutil.pids(): print x p = psutil.Process(x) if p.name() == 'python': print "OK -> {}".format(x) proc_watchdog = x #print p.name() ''' p.exe() p.cwd() p.status() p.cmdline() p.status() p.username() p.create_time() p.terminal() p.uids() p.gids() p.cpu_times() p.cpu_percent(interval=1) # seconds p.cpu_affinity() p.memory_percent() p.memory_info() p.memory_info_ex() p.memory_maps() p.io_counters() p.open_files() p.connections() p.num_threads() p.num_fds() p.num_ctx_switches() p.nice() # this value gives a process more or less cpu [-20(highest priority -> 19(lowest)] # p.nice(10) # set the nice of a certain process p to 10 # p.ionice() # set input output nice # p.rlimit() # resource limit # p.suspend() # p.resume() # p.terminate() # p.wait() ''' print "\npsutil.test()" print psutil.test() print "#### PROCESS MANAGEMENT/END ####" ######################################### POST PROCESSS print "#### POST PROCESS ####" print "\npsutil.process_iter()" for p in psutil.process_iter(): print(p) def on_proc_terminate(proc): print ("Do _{}_ post process procedure ".format(proc)) # waits for multiple process to terminate wait_timeout = 3 # seconds gone, alive = psutil.wait_procs(psutil.process_iter(), wait_timeout, callback=on_proc_terminate) print "\n gone" print gone print "\n alive" print alive print "do it for a single process" p = psutil.Process(proc_watchdog) try: p.wait(timeout=wait_timeout) except psutil.TimeoutExpired: p.kill() try: p.wait(timeout=wait_time) except psutil.TimeoutExpired: sys.exit('giving up xD') print "#### POST PROCESS/END ####"
print "\n" print "net_if_stats is ",psutil.net_if_stats() print "boot_time is ",psutil.boot_time() print "pids is ",psutil.pids() p=psutil.Process(8924) print p.name() print p.exe() print "cwd is ",p.cwd() print "cmdline is ",p.cmdline() print p.status() print p.username() print p.create_time() print p.memory_full_info() print p.memory_info() print "io_counters is",p.io_counters() print p.open_files() print p.connections() print p.num_ctx_switches() print p.nice() print psutil.test() for p in psutil.process_iter(): print p for i in list(psutil.win_service_iter()): print i
import psutil as ps print(f'CPU INFO \n Cores - {ps.cpu_count()} Frequency - {ps.cpu_freq()} \n CPU % utilization - {ps.cpu_percent()}') print(f'All INFO of RAM \n {ps.virtual_memory()}, \n swap memory: {ps.swap_memory()}') print(f'File system mounted on "/" \n {ps.disk_usage("/")}') for el,v in ps.net_if_addrs().items(): print(f'Interface: {el} ---- {v}') print(f'{"*"*100}') print(ps.net_io_counters()) print(ps.test())
ps.cpu_percent(interval=1, percpu=True) # 获取内存信息,返回的字节为单位的整数 ps.virtual_memory() # 物理内存信息 ps.swap_memory() # 交换内存信息 # 获取磁盘信息 ps.disk_partitions() # 磁盘分区信息 ps.disk_usage('c:') # 磁盘使用情况 linux下,磁盘用"/" ps.disk_io_counters() # 磁盘IO # 获取网络信息 ps.net_io_counters() # 获取网络读写字节/包的个数 ps.net_if_addrs() # 获取网络接口信息 ps.net_if_stats() # 获取网络接口状态 ps.net_connections() # 获取当前网络信息 # 获取进程信息 ps.pids() # 所有进程ID ps.test() # 类似linux的ps p = ps.Process(ps.pids()[0]) # 获取指定ID p.name() # 进程名称 p.exe() # 进程exe路径 p.cwd() # 进程工作目录 p.cmdline() # 进程启动的命令行 p.ppid() # 父进程ID p.parent() # 父进程 p.children() # 子进程列表 p.status() # 进程状态 p.username() # 进程用户 p.create_time() # 进程创建时间 p.terminal() # 进程终端 p.cpu_times() # 进程使用CPU时间 p.memory_info() # 进程占用内存 p.open_files() # 进程打开的文件
import psutil, time #l = psutil.pids() #for pid in l: # p = psutil.Process(pid) # print p.name() f = open('tempFile', 'a') f.write(psutil.test()) f.close()
def show_process(): # 进程 psutil.test()
print("Used Space: ", (SizeConverter(usage[1]))) print("Free Space: ", (SizeConverter(usage[2])), "\n ") except Exception as e: print(e) print( "Drive unable to be scanned. Usually empty CDROM or Floppy drive.", "\n") pass print(" ") print("Currently Active User:"******"\n ") print(psutil.users(), "\n ", "\n ") print("System Process Table:", "\n ") print(psutil.test(), "\n ", "\n ") print("Network Interfaces:", "\n ") pprint.pprint(psutil.net_if_stats()) print(" ", "\n ", "\n ") print("Network Interface Address Information: ", "\n ") pprint.pprint(psutil.net_if_addrs()) print(" ", "\n ", "\n ") print("System Socket Information: ", "\n ") pprint.pprint(psutil.net_connections()) print(" ", "\n ", "\n ") #Function to identify platform then run os specific file structure commands if platforminfo[0].lower() == "windows":
# 获取网络信息 print(psutil.net_io_counters()) # 获取网络读写字节/包的个数 print(psutil.net_if_addrs()) # 获取网络接口信息 print(psutil.net_if_stats()) # 获取网络接口状态 print(psutil.net_connections()) # 获取当前网络连接信息 # 获取进程信息 print(psutil.pids()) # 所有进程ID p = psutil.Process(10468) # 获取指定进程ID=3776 print(p.name()) # 进程名称 print(p.exe()) # 进程exe路径 print(p.cwd()) # 进程工作目录 print(p.cmdline()) # 进程启动命令 print(p.ppid()) # 父进程ID print(p.parent()) # 父进程 print(p.children()) # 子进程列表 print(p.status()) # 进程状态 print(p.username()) # 进程用户名 print(p.create_time()) # 进程创建时间 # print(p.terminal()) # 进程终端 print(p.cpu_times()) # 进程使用的CPU时间 print(p.memory_info()) # 进程使用的内存 print(p.open_files()) # 进程打开的文件 print(p.connections()) # 进程相关网络连接 print(p.num_threads()) # 进程的线程数量 print(p.threads()) # 所有进程信息 print(p.environ()) # 进程环境变量 # print(p.terminate()) # 结束进程 print(psutil.test()) # ps效果
print("-----------cpu_freq-----------") print(psutil.cpu_freq()) # 返回CPU频率 print("-----------cpu_percent-----------") print(psutil.cpu_percent()) # 返回当前系统CPU利用率(float类型) print("-----------cpu_stats-----------") print(psutil.cpu_stats()) # 返回CPU统计数据 print("-----------cpu_times-----------") print(psutil.cpu_times()) # 返回系统范围内的CPU时间。 print("-----------cpu_times_percent-----------") print(psutil.cpu_times_percent()) # 对于每个特定的CPU时间提供利用率,由cpu_times()返回。 print("-----------boot_time-----------") print(psutil.boot_time()) # 返回从1970以来以秒表示的系统启动时间 print("-----------users-----------") print(psutil.users()) # 返回当前连接在系统上的用户列表 print("-----------test-----------") print(psutil.test()) # 列出所有当前正在运行的进程信息 # """ # 结果 # -----------cpu_count----------- # 4 # -----------cpu_freq----------- # scpufreq(current=3168.0, min=0.0, max=3201.0) # -----------cpu_percent----------- # 0.0 # -----------cpu_stats----------- # scpustats(ctx_switches=861048211, interrupts=148032987, soft_interrupts=0, syscalls=1186688671) # -----------cpu_times----------- # scputimes(user=6015.96044921875, system=2641.171875, idle=79255.78125, interrupt=93.52259969711304, dpc=523.3521823883057) # -----------cpu_times_percent----------- # scputimes(user=0.0, system=0.0, idle=0.0, interrupt=0.0, dpc=0.0)
import psutil import sys import io if __name__ == '__main__': sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8') f = open("test.txt", 'w') old = sys.stdout sys.stdout = f print(psutil.test().decode('gbk')) sys.stdout = old f.close()
import psutil psutil.test()
# print(psutil.net_if_addrs()) # 网络接口信息 # print(psutil.net_if_stats()) # 网络接口状态 # for i in psutil.net_connections(): # print(i, '\n') # 当前网络连接信息 需要sudo # 4.Process information: # print(psutil.pids()) # 所有进程pid p = psutil.Process(444) # 获取指定pid进程 # print(p.name()) # 进程名称 # print(p.exe()) # 进程exe路径 # print(p.cwd()) # 进程工作目录 # print(p.cmdline()) # 进程启动的命令行 # print(p.ppid()) # 父进程ID # print(p.parent()) # 父进程 # print(p.children()) # 子进程列表 # print(p.status()) # 进程状态 # print(p.username()) # 进程用户名 # print(p.create_time()) # 进程创建时间 # print(p.terminal()) # 进程终端 # print(p.cpu_times()) # 进程使用的CPU时间 # print(p.memory_info()) # 进程使用的内存 # print(p.open_files()) # 进程打开的文件 # print(p.connections()) # 进程相关网络连接 # print(p.num_threads()) # 进程的线程数量 # print(p.threads()) # 所有线程信息 # print(p.environ()) # 所有线程信息 # print(p.terminate()) # 结束进程 print(psutil.test()) # 类似于ps命令
def top(self): print(psutil.test())
# client.py import subprocess import re from psutil import test import socket import time # current processes a = test().hide() # create a socket object s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # get local machine name host = '192.168.0.3' port = 9999 # connection to hostname on the port. s1.bind((host, port)) # Receive no more than 1024 bytes #while 1: #print (s.recv(1024)).decode('ascii') #s.send(raw_input("Chatiing ==> ")) #print("The time got from the server is %s" % tm.decode('ascii')) arr = "" p = subprocess.Popen('ls',shell = True , stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
# psutil.pids() 以列表的形式返回当前正在运行的进程 # psutil.pid_exists(1) 判断给点定的pid是否存在 # psutil.process_iter() 迭代当前正在运行的进程,返回的是每个进程的Process对象 # psutil.Process() 对进程进行封装,可以使用该类的方法获取进行的详细信息,或者给进程发送信号。 print(psutil.pids()) # print(*psutil.process_iter()) # print(datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))#开机时间 p = psutil.Process(1060) # 进程名称 print(p.name()) #获取进程交互环境 print(p.exe()) #获取进程路径 print(p.cwd()) #获取进程工作目录 print(p.cmdline()) #获取进程启动的工作行 print(p.ppid()) #获取父进程id print(p.parent()) #获取父进程 # p = psutil.Process(1000) # print(p.children())#获取子进程 print(p.status()) #获取进程状态 print(p.username()) #获取进程用户名 print(p.create_time()) #获取进程创建时间 print(p.terminal()) #获取进程终端 print(p.cpu_times()) #获取进程使用时间 print(p.memory_info()) #获取进程使用内存 print(p.open_files()) #获取进程打开的文件 print(p.connections()) #获取进程相关的网络链接 print(p.num_threads()) #获取进程的线程数量 print(p.threads()) #获取所以线程信息 print(p.environ()) #获取进程环境变量 print(p.terminate()) #结束进程 print(psutil.test()) #所有信息
def process(self, widget, data=None): self.window1 = gtk.Window(type=gtk.WINDOW_TOPLEVEL) self.window1.set_title("Processes") print 'All Running Processes : ' print psutil.test()
def details(): return psutil.test()
def ProcessHandler(self): process = (psutil.test()) self.clientsocket.send(str(process)) time.sleep(1)
print('\n状态', psutil.net_if_stats()) #网络接口状态 print('\n当前连接状态', psutil.net_connections()) # AccessDenied错误:原因是psutil获取信息也是要走系统接口,而获取网络连接信息需要root权限,这种情况下,可以退出Python交互环境,用sudo重新启动: # 进程信息 print('\n\n所有进程 ', psutil.pids()) p = psutil.Process(5712) #指定进程 print('\n[5712]name = ', p.name()) print('[5712] = ', p.exe()) print('[5576]工作目录 = ', p.cwd()) print('[5576]命令行 = ', p.cmdline()) print('[5576]命令行 = ', p.cmdline()) print('[5576]父进程 = ', p.parent()) print('[5576]父进程ID = ', p.ppid()) print('[5576]子进程ID = ', p.children()) print('[5576]进程状态 ', p.status()) print('[5576]进程用户名 ', p.username()) # print('[5568]进程终端 ',p.terminal()) print('[5576]cpu时间 ', p.cpu_times()) print('[5576]内存 ', p.memory_info()) print('[5576]打开文件 ', p.open_files()) print('[5576]相关网络连接 ', p.connections()) print('[5576]线程 ', p.threads()) print('[5576]线程数量 ', p.num_threads()) print('[5576]环境变量 ', p.environ()) # print('[5576]结束进程 ',p.terminate()) # psutil还提供了一个test()函数,可以模拟出ps命令的效果: print('\n', psutil.test())
# print(psutil.net_io_counters()) # 获取网络读写字节/包的个数 # print(psutil.net_if_addrs()) # 获取网络接口信息 # print(psutil.net_if_stats()) # 获取网络接口状态 # print(psutil.net_connections()) # 获取当前网络连接信息 # 获取进程信息 # print(psutil.pids()) # 所有进程ID # p = psutil.Process(0) # 获取指定进程ID=3776,其实就是当前Python交互环境 # print(p.name()) # 进程名称 # print(p.exe()) # 进程exe路径 # print(p.cwd()) # 进程工作目录 # print(p.cmdline()) # 进程启动的命令行 # print(p.ppid()) # 父进程ID # print(p.parent()) # 父进程 # print(p.children()) # 子进程列表 # print(p.status()) # 进程状态 # print(p.username()) # 进程用户名 # print(p.create_time()) # 进程创建时间 # print(p.terminal()) # 进程终端 # print(p.cpu_times()) # 进程使用的CPU时间 # print(p.memory_info()) # 进程使用的内存 # print(p.open_files()) # 进程打开的文件 # print(p.connections()) # 进程相关网络连接 # print(p.num_threads()) # 进程的线程数量 # print(p.threads()) # 所有线程信息 # print(p.environ()) # 进程环境变量 # print(p.terminate()) # 结束进程 print(psutil.test()) # 模拟出ps命令的效果
# 1. 获取CPU信息 print('cpu_count-->', psutil.cpu_count()) print('cpu_freq-->', psutil.cpu_freq()) print('cpu_percent-->', psutil.cpu_percent()) print('cpu_stats-->', psutil.cpu_stats()) print('cpu_times-->', psutil.cpu_times()) # 2. 实现类似top命令的CPU使用率 for x in range(2): r = psutil.cpu_percent(interval=1, percpu=True) print(r) # 3. 获取内存信息 print('virtual_memory-->', psutil.virtual_memory()) print('swap_memory-->', psutil.swap_memory()) # 4. 获取磁盘信息 print('disk_partitions-->', psutil.disk_partitions()) print('disk_usage-->', psutil.disk_usage('/')) print('disk_io_counters-->', psutil.disk_io_counters()) # 5. 获取网络信息 print('net_io_counters-->', psutil.net_io_counters()) # 获取网络读写字节/包的个数 print('net_if_addrs-->', psutil.net_if_addrs()) # 获取网络接口信息 print('net_if_stats-->', psutil.net_if_stats()) # 获取网络接口状态 print('net_connections-->', psutil.net_connections()) # 获取当前网络连接信息 # 6. 获取进程信息 print('pids-->', psutil.pids()) print('test-->', psutil.test())
# @File : psutil_test.py # @Software: IntelliJ IDEA import psutil # CPU逻辑数量 c = psutil.cpu_count() print(c) # CPU物理核心 r = psutil.cpu_count(logical=False) print(r) # 2说明是双核超线程,4则是4核非超线程 print(psutil.cpu_times()) # for x in range(10): # print(psutil.cpu_percent(interval=1, percpu=True)) print(psutil.virtual_memory()) print(psutil.swap_memory()) print(psutil.disk_partitions()) print(psutil.disk_usage("/")) print(psutil.disk_io_counters()) print(psutil.net_io_counters()) print(psutil.net_if_addrs()) print(psutil.net_if_stats()) # print(psutil.net_connections()) print(psutil.pids()) print(psutil.test())
# 显示网络信息 print(psutil.net_io_counters()) # 获取网络读写字节/包的个数 print(psutil.net_if_addrs()) # 获取网络接口信息 print(psutil.net_if_stats()) # 获取网络接口状态 print(psutil.net_connections()) # 获取当前网络连接信息 print('*' * 100) # 显示进程信息 print(psutil.pids()) # 所有进程ID p = psutil.Process(32) print(p.name()) # 进程名称 print(p.exe()) # 进程exe路径 print(p.cwd()) # 进程工作目录 print(p.cmdline()) # 进程启动的命令行 print(p.ppid()) # 父进程ID print(p.parent()) # 父进程 print(p.children()) # 子进程列表 print(p.status()) # 进程状态 print(p.username()) # 进程用户名 print(p.create_time()) # 进程创建时间 print(p.cpu_times()) # 进程使用的CPU时间 print(p.memory_info()) # 进程使用的内存 print(p.open_files()) # 进程打开的文件 print(p.connections()) # 进程相关网络连接 print(p.num_threads()) # 进程的线程数量 print(p.threads()) # 所有线程信息 print(p.environ()) # 进程环境变量 print(psutil.test()) # 类似Linux的ps print('*' * 100)
def Process(): print("=" * 35, "All Process ", "=" * 35) psutil.test() input("\nPress any key to continue.. ") sp.call('cls', shell=True)
print("network: ", psutil.net_connections()) # 当前网络连接信息 # 进程 print("process: ", psutil.pids()) # 所有进程ID p = psutil.Process(12052) # 获取指定进程 print("process: ", p.name(), # 进程名称 "\nprocess: ", p.status(), # 进程状态 "\nprocess: ", p.exe(), # 进程exe路径 "\nprocess: ", p.cwd(), # 进程工作目录 "\nprocess: ", p.create_time(), # 进程创建时间 "\nprocess: ", p.cmdline(), # 进程启动的命令行 "\nprocess: ", p.ppid(), # 父进程ID "\nprocess: ", p.parent(), # 父进程 "\nprocess: ", p.children(), # 子进程列表 "\nprocess: ", p.username(), # 进程用户名 "\nprocess: ", p.cpu_times(), # 进程使用的CPU时间 "\nprocess: ", p.memory_info(), # 进程使用的内存 "\nprocess: ", p.num_threads(), # 进程的线程数量 "\nprocess: ", p.threads(), # 所有线程信息 "\nprocess: ", p.environ(), # 进程环境变量 "\nprocess: ", p.open_files(), # 进程打开的文件 "\nprocess: ", p.connections() # 进程相关网络连接 ) # p.terminate() # 结束进程 psutil.test() # test()函数可模拟出ps命令的效果 # ### psutil # - Cross-platform lib for process and system monitoring in Python. # - Home Page: https://github.com/giampaolo/psutil # - Documentation: http://psutil.readthedocs.io/en/latest/
threading.Lock().acquire() watcher(self.fileName) threading.Lock().release() processWatch = myWatch('logs/processList.out') statWatch = myWatch('logs/Status_Log.txt') processWatch.start() statWatch.start() # enter the processes data to a file processList = open('logs/processList.out', 'w+') processList.write('Time: ') processList.write(str(datetime.datetime.now())) processList.write('\n') sys.stdout = processList psutil.test() sys.stdout = sys.__stdout__ os.chmod('logs/processList.out', 0600) os.chmod('logs/Status_Log.txt', 0600) current = list() processList = open('logs/processList.out', 'r') for line in processList: current.append(line) processList.close() current = current[1:] while True: # updating files prev = list() processList = current
# p.parent() # 父进程 # print(p.parent()) # p.children() # 子进程列表 # print(p.children()) # p.status() # 进程状态 # print(p.status()) # p.username() # 进程用户名 # print(p.username()) # p.create_time() # 进程创建时间 # print(p.create_time()) # # p.terminal() # 进程终端 # p.cpu_times() # 进程使用的CPU时间 # p.memory_info() # 进程使用的内存 # p.open_files() # 进程打开的文件 # p.connections() # 进程相关网络链接 # p.num_threads() # 进程的线程数量 # print(p.num_threads()) # p.threads() # 所有线程信息 # print(p.threads()) # # p.environ() # 进程环境变量 # p.terminate() # 结束进程 # print(p.terminate()) # 和获取网络连接类似,获取一个root用户的进程需要root权限,启动Python交互环境或者.py文件时,需要sudo权限。 # psutil还提供了一个test()函数,可以模拟出ps命令的效果: import psutil psutil.test() # 模拟ps命令的效果,即用ps命令用来列出系统中当前运行的那些进程 # psutil使得Python程序获取系统信息变得易如反掌。 # psutil还可以获取用户信息、Windows服务等很多有用的系统信息 # 具体请参考psutil的官网:https://github.com/giampaolo/psutil