jsmubanlogo
  • 首页
  • 网页模板
  • 特效代码
  • 博文源码
  • 插件下载
  •    

python判断进程是否存在

收藏    

作者第十天    2022-05-03

      

1. 进程名 

import psutil
 
# pl = psutil.pids()
# for pid in pl:
#     if psutil.Process(pid).name() == 'notepad++.exe':
#         print(pid)
 
 
def checkprocess(processname):
    pl = psutil.pids()
    for pid in pl:
        if psutil.Process(pid).name() == processname:
            return pid
 
# print(isinstance(checkprocess("notepad++.exe"),int))
 
if isinstance(checkprocess("notepad++.exe"),int):
    print("进程存在")
else:
    print("进程不存在")

 2. 进程ID

import errno
import os
import sys
 
 
 
def pid_exists(pid):
    """Check whether pid exists in the current process table.
    UNIX only.
    """
    if pid < 0:
        return False
    if pid == 0:
        # According to "man 2 kill" PID 0 refers to every process
        # in the process group of the calling process.
        # On certain systems 0 is a valid PID but we have no way
        # to know that in a portable fashion.
        raise ValueError('invalid PID 0')
    try:
        os.kill(pid, 0)
    except OSError as err:
        if err.errno == errno.ESRCH:
            # ESRCH == No such process
            return False
        elif err.errno == errno.EPERM:
            # EPERM clearly means there's a process to deny access to
            return True
        else:
            # According to "man 2 kill" possible error values are
            # (EINVAL, EPERM, ESRCH)
            raise
    else:
        return True

利用while判断是否存在,如果不存在则睡眠10秒 

 while  isinstance(checkprocess("chrome.exe"), int) < 0:
        time.sleep(10)

版权声明:本文为CSDN博主「墨痕诉清风」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u012206617/article/details/85118187


免责声明:
      1、 资源售价只是赞助,不代表代码或者素材本身价格。收取费用仅维持本站的日常运营所需。
      2、 本站资源来自用户上传,仅供用户学习使用,不得用于商业或者非法用途,违反国家法律一切后果用户自负。用于商业用途,请购买正版授权合法使用。
      3、 本站资源不保证其完整性和安全性,下载后自行检测安全,在使用过程中出现的任何问题均与本站无关,本站不承担任何技术及版权问题,不对任何资源负法律责任。
      4、 如有损害你的权益,请联系275551777@qq.com及时删除。

关于我们 | 积分获取 | 联系我们 | 用户协议 | 标签搜索 | 网站地图.html | 网站地图.xml | 网站地图.txt

Copyright © 2021-2023 All Right Reserved
陕公网安备 61082202000148号      陕ICP备2025078528号-1
js模板网 -陕西千手码农科技有限责任公司