pustil模块
psutil是一个跨平台库(http://pythonhosted.org/psutil/)能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。它主要用来做系统监控,性能分析,进程管理。
使用pustil库之前,需要先安装,安装命令如下:
pip install pustil
进程信息
pids = psutil.pids()
for pid in pids:
p = psutil.Process(pid)
# get process name according to pid
process_name = p.name()
print("Process name is: %s, pid is: %s" %(process_name, pid))
可以查看的进程信息:
p.name() #进程名
p.exe() #进程的bin路径
p.cwd() #进程的工作目录绝对路径
p.status() #进程状态
p.create_time() #进程创建时间
p.uids() #进程uid信息
p.gids() #进程的gid信息
p.cpu_times() #进程的cpu时间信息,包括user,system两个cpu信息
p.cpu_affinity() #get进程cpu亲和度,如果要设置cpu亲和度,将cpu号作为参考就好
p.memory_percent() #进程内存利用率
p.memory_info() #进程内存rss,vms信息
p.io_counters() #进程的IO信息,包括读写IO数字及参数
p.connections() #返回进程对象的列表
p.num_threads() #进程开启的线程数p.username() #执行用户的名
杀掉指定进程
linux系统
import signal
pids = psutil.pids()
for pid in pids:
p = psutil.Process(pid)
# get process name according to pid
process_name = p.name()
# kill process "sleep_test1"
if 'sleep_test1' == process_name:
print("kill specific process: name(%s)-pid(%s)" %(process_name, pid))
os.kill(pid, signal.SIGKILL)
exit(0)
报错:
AttributeError: module 'signal' has no attribute 'SIGKILL'
参考文章AttributeError: module ‘signal‘ has no attribute ‘SIGKILL‘,修改上面代码:
#os.kill(pid, signal.SIGKILL)
os.kill(pid, signal.INT)
依然报错:
Traceback (most recent call last):
File "C:\\Users\\xrw020\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\psutil\\_pswindows.py", line 679, in wrapper
return fun(self, *args, **kwargs)
File "C:\\Users\\xrw020\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\psutil\\_pswindows.py", line 933, in create_time
user, system, created = cext.proc_times(self.pid)
ProcessLookupError: [Errno 3] assume no such process (originated from OpenProcess -> ERROR_INVALID_PARAMETER)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\\Users\\xrw020\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\psutil\\__init__.py", line 354, in _init self.create_time()
File "C:\\Users\\xrw020\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\psutil\\__init__.py", line 710, in create_time
self._create_time = self._proc.create_time()
File "C:\\Users\\xrw020\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\psutil\\_pswindows.py", line 681, in wrapper
raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.NoSuchProcess: psutil.NoSuchProcess process no longer exists (pid=17184)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 128, in <module>
KilWebServerProcess(casepath)
File "main.py", line 41, in KilWebServerProcess
p = psutil.Process(pid)
File "C:\\Users\\xrw020\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\psutil\\__init__.py", line 326, in __init__
self._init(pid)
File "C:\\Users\\xrw020\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\psutil\\__init__.py", line 367, in _init raise NoSuchProcess(pid, None, msg)
psutil.NoSuchProcess: psutil.NoSuchProcess no process found with pid 17184
分析原因:os.kill()是linux系统杀死进程的方法,对于windows系统并不适用
windows系统
#根据进程名杀死进程
pro = 'taskill /f /im %s'% process_name
os.system(pro)
#根据pid杀死进程
process = 'taskill /f /pid %s'%pid
os.system(process)
相关文献
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容