1

before going further ,one question... is it possible to do it ?

If the answer is yes... then which library, and which is the best practice to implement it on a python script.

If the answer is no, by which language is it possible, that i start learning it enough to proceed my project.

Project Description : I want to write program(using classes) to stop the desired windows programs from running.

The only thing I need is to know is which library, which module and which class or function in order to open or stop apps from running using python(mainly closing the windows programs).

Note:The reason why i did not mention the code was for its long length, and the fact that it did not help, because the only thing which matters is which library and module to use.

3
  • @Dan, could you please tell me how to include the program that i want to exit from in the function ? Commented Oct 11, 2019 at 15:04
  • On Windows I think you would need to execute (via os.system or the subprocess module) the builtin Windows taskkill command — and don't think os.kill would work on that OS. Also note that killing a process isn't quite the same as making it exit. Commented Oct 11, 2019 at 15:08
  • 1
    @martineau, os.kill works. It opens a handle to the process and calls TerminateProcess with the given exit status, so it's like a Unix SIGKILL. But for an exit status of 0 or 1 it's conflated with calling GenerateConsoleCtrlEvent to send CTRL_C_EVENT (0) or CTRL_BREAK_EVENT (1) to a process group that's attached to the current console. Avoid that by using an exit status that's greater than 1. That said, a forceful termination should be a last resort. The process should be given a chance to exit cleanly, and Python's os.kill doesn't support this, unlike "taskkill.exe". Commented Oct 11, 2019 at 16:49

1 Answer 1

4

psutil provides an abstract interface to process related API. It's a cross-platform library allows you retrieve the processes and system running state information such as CPU, memory, disk usage, network, etc. It's a great tool for system monitoring and administration.

import psutil

for proc in psutil.process_iter():
    print(proc.name())

def kill_by_process_name(name):
    for proc in psutil.process_iter():
        if proc.name() == name:
            print("Killing process: " + name)
            if(check_process_exist_by_name(name)):
                print("Killing process: " + name + " sucess")
            else:
                print("Killing process: " + name + " failed")
            return

    print("Not found process: " + name)

def check_process_exist_by_name(name):
    for proc in psutil.process_iter():
        if proc.name() == name:
            return True

    return False

kill_by_process_name("iTunesHelper.exe")    

and then you could use the os module with 'taskkill' to kill the task.

import os

def kill_by_process_name_shell(name):
    os.system("taskkill /f /im " + name)

kill_by_process_name_shell("iTunesHelper.exe")

Referenced from here

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

7 Comments

Thank you... it really helped.
Accept the answer maybe? :)
I cant do it until 6 min later, according to stackoverflow's rules:)))
Glad I could help!
I use MacOS. So I'm not exactly sure, but I believe if you do kill_by_process_name("iTunesHelper.*") Or kill_by_process_name("iTunesHelper*") it should work. It's called "Wildcards"
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.