1

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.

7
  • Do you know how to use either WMI or Win32 API? If so, there's a WMI events for process launch that you can wait on, or you can install a Windows hook that catches every new window and see which window class name it's for. But neither one is trivial to learn, so you're going to have to do some research before you can do anything with an answer. Commented Apr 6, 2018 at 20:42
  • Alternatively, a quick&dirty solution is to just 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? Commented Apr 6, 2018 at 20:44
  • Sorry, I should have specified, but I've hid the ability to close the script from the user, but they can still access task manager and close the script that way(windows doesn't let you block) so I do need to exit it immediately. I think I will look into your first suggestion if no other alternative exists Commented Apr 6, 2018 at 20:49
  • Your edit makes no sense. Your script is running continuously, and can be easily ended through Task Manager. Whether it kills Steam instantly or after half a second doesn't change that either way. Commented Apr 6, 2018 at 20:49
  • Also, there's a name for a program that can't be killed easily by the user: malware. Commented Apr 6, 2018 at 20:50

2 Answers 2

3

Okay, so to help anyone that might come across this question, I got it working using a wmi listener from abarnert's suggestion:

import os
import pythoncom
from threading import Thread
import time
import wmi

def listener():
    pythoncom.CoInitialize()
    c = wmi.WMI()
    process_watcher = c.Win32_Process.watch_for("creation")
    while True:
        new_process = process_watcher()
        if new_process.Caption == 'myApplication.exe':
            os.system('taskkill /im myApplication.exe')


active_time = int(input('How long would you like to block programs for?'))

t = Thread(target=listener)
t.daemon = True
t.start()

time.sleep(active_time)

This has CPU usage down to only a couple percent and works just as well.

Sign up to request clarification or add additional context in comments.

Comments

1

You currently execute external app constantly what eats a lot of resources. import sleep from time and put the sleep(1) or sleep(5) inside your while loop. this will reduce your cpu usage.

something like:

from time import sleep
...your imports...

while True:
    p_tasklist........
    if matches your criteria:
        close program
    sleep(number_of_seconds)

2 Comments

I updated it so the for loop is outside of the while loop, which was a good suggestion. Now it only runs if the task list changes. But how can I reuse the tasklist? Mustn't I update it to determine if the unwanted program is running? Also, I should have specified, but I need the unwanted program to be closed as fast as possible (this stops people from ending the application early though task manager) so I cannot use sleep in the loop.
My apologies, I misunderstood the tasklist as list of prohibited tasks. The sleep itself should reduce the cpu usage enough to help you avoid any further modifications. I edited my answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.