-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessBlocker.py
38 lines (32 loc) · 1.27 KB
/
ProcessBlocker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import psutil
import time
import os
# Application to Block a provided exe/process name from running
class ProcessBlocker:
def __init__(self, names, interval, timerminutes=-1):
self.process = names
self.checkInterval = interval
self.endTime = int(time.time() + timerminutes * 60) if timerminutes >= 0 else None
# returns array of true/false if any process in this.process is running
def processIsRunning(self):
checks = [False for i in self.process]
for p in psutil.process_iter():
if p.name() in self.process:
checks[self.process.index(p.name())] = True
return checks
# Will attempt to kill/end a process with any of this.process
def killProcess(self, checks):
for i in range(0, len(checks)):
if checks[i]:
try:
os.system("taskkill /f /im " + self.process[i])
except:
pass
# Checks if processing is running, or sleeps if not
def waitForProcess(self):
while self.endTime is None or time.time() <= self.endTime:
checks = self.processIsRunning()
if True in checks:
self.killProcess(checks)
else:
time.sleep(self.checkInterval)