Python Forum
psutil for monitor a software finish process and notification - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: psutil for monitor a software finish process and notification (/thread-25356.html)



psutil for monitor a software finish process and notification - Pyguys - Mar-27-2020

psutil for monitor a software finish process and notification

import psutil, time
PROCNAME = "monitor.exe"

for proc in psutil.process_iter():
    if proc.name() == PROCNAME:
        print(proc.pid)
        softwarepid=proc.pid
        time.sleep(0.1)
        pi = psutil.Process(softwarepid)
        softwarepercent = pi.cpu_percent(interval=1.0)
        print(softwarepercent)
Error:
Traceback (most recent call last): File "C:\Users\xx\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psutil\_common.py", line 449, in wrapper ret = self._cache[fun] AttributeError: _cache During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\xx\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psutil\_pswindows.py", line 679, in wrapper return fun(self, *args, **kwargs) File "C:\Users\xx\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psutil\_common.py", line 452, in wrapper return fun(self) File "C:\Users\xx\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psutil\_pswindows.py", line 766, in exe exe = cext.proc_exe(self.pid) PermissionError: [WinError 24] The program issued a command but the command length is incorrect: '(originated from NtQuerySystemInformation)' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:/Users/xx/PycharmProjects/app/bee.py", line 29, in <module> if proc.name() == PROCNAME: File "C:\Users\xx\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psutil\__init__.py", line 630, in name name = self._proc.name() File "C:\Users\xx\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\xx\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=2928)
same code test in different pc, same version pycharm and win. some can get result and error. some can't get result and give same error. what's wrong with it why can't get result? and get CPU percent function seems no right way.

my idea: as this software don't have notification function. so i want to make python monitor it. monitor it CPU usage, when it stop working. it will stay in 0.0% then send email and sounds notification. i am no sure it's the right way. please advice.