P
P
Peter Pitaev2021-05-30 12:54:03
Python
Peter Pitaev, 2021-05-30 12:54:03

Python how to terminate .exe process properly?

Hello.
I launch firefox shortcuts with the profiles I need, up to 10 pieces at the same time . Now after a certain time they need to be completed, for example, the third profile, then the fifth, the first, etc. I still have not figured it out. I figured out how to kill all firefox.exe processes: But the next time I start it, firefox writes that the work was not completed correctly and asks to start in safe mode. With alt+f4, it doesn't swear at an incorrect shutdown. The question is how to complete it correctly so that he does not swear? And even better, how to complete a certain profile correctly?

os.startfile(f"profile{i}.lnk")


os.system("taskkill /im firefox.exe")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Hemul GM, 2021-05-30
@XXocTT

Send a message to the application to close via WinAPI

Y
Yupiter7575, 2021-05-30
@yupiter7575

os.system("taskkill /im firefox.exe")

This is bad practice. Much better to do this:
import subprocess as sp
sp.Popen('taskkill /im firefox.exe /f')

If you run through the python interpreter, if you have a packaged .py file, then it's better to do this:
import psutil #pip isntall psutil

for proc in psutil.process_iter():
    if proc.name() == 'firefox.exe':
        proc.terminate()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question