I'd like to run a python script for a long period of time in the background. Basically, I'm trying to make a productivity script, that will force close distracting applications while the program is running. So that, for example, the user enters 30 minutes, and for the next 30 minutes the program will run, and force close things like Steam or Google Chrome.
Currently, I have it working using a while loop with a daemon thread, that keeps checking the running applications and closes one if it needs to:
while True:
p_tasklist = subprocess.Popen('tasklist.exe /fo csv',
stdout=subprocess.PIPE,
universal_newlines=True)
for p in csv.DictReader(p_tasklist.stdout):
if p['Image Name'] == 'myProgram.exe':
os.system('taskkill /im myProgram.exe')
But obviously this is very CPU intensive (one CPU core is at 100%), and I was trying to somehow set up an event listener that listens for new programs to be opened, but have been unsuccessful so far.
I was trying to make something along the lines of this work:
while True
# listen for new program to be opened then run next block
if program == program_to_close:
close program
edit: To prevent people from ending my script prematurely, I need the unwanted program the be closed as fast as possible. Otherwise, the script can be ended through task manager, kinda making the whole thing pointless for those with little self control.
edit 2: Task manager is one of the programs I wish to force close, not Steam specifically. I'm not trying to make malware or disable someones computer, if they absolutely needed to exit, they could just restart the computer.
sleep(15)each time through the loop. Or maybe you only want to sleep for half a second rather than 15 seconds; you can tweak it until it feels right. But surely you don't need to kill Steam in the first microsecond in order to accomplish your goal, right?