简体   繁体   English

psutil.AccessDenied: psutil.AccessDenied (pid=7868)

[英]psutil.AccessDenied: psutil.AccessDenied (pid=7868)

I'm trying to kill a process (zoom.exe) with psutil .我正在尝试使用psutil 终止进程 (zoom.exe)。 However, I'm getting an error psutil.AccessDenied .但是,我收到错误psutil.AccessDenied I have all the permissions to do it, and was already tested on another pc.我拥有执行此操作的所有权限,并且已经在另一台电脑上进行了测试。

Code代码

def kill_process(PROCNAME):
  for proc in psutil.process_iter():
    if proc.name() == PROCNAME:
      proc.kill()
      print(f'{PROCNAME} was succesfully killed')
  time.sleep(3)

Traceback追溯

Traceback (most recent call last):
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psutil\_c
    ret = self._cache[fun]
AttributeError: _cache

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psutil\_p
    return fun(self, *args, **kwargs)
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psutil\_c
    return fun(self)
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psutil\_p
    exe = cext.proc_exe(self.pid)
PermissionError: [WinError 24] The program issued a command but the command length is incorrect.
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:/Users/Acer/Desktop/autozoom/autozoom.py", line 211, in <module>
    kill_process('Zoom.exe')
  File "c:/Users/Acer/Desktop/autozoom/autozoom.py", line 25, in kill_process
    if proc.name() == PROCNAME:
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psutil\__init__.py", line 630, in name
    name = self._proc.name()
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psutil\_pswindows.py", line 750, in name
    return os.path.basename(self.exe())
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psutil\_pswindows.py", line 681, in wrapper
    raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=7868)

Solved the issue with this code:解决了此代码的问题:

def kill_process(PROCNAME):
  for proc in psutil.process_iter():
    try:
      if proc.name().lower() == PROCNAME.lower():
        proc.kill()
        return True
    except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
      return False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM