How can I run an exe file from Python 3.x Windows Service?

点点圈 提交于 2019-12-24 23:12:22

问题


This is very frustrating. I am trying to launch any EXE from a WINDOWS SERVICE. I'm using Python 3.5 and Win10. I created the service inheriting from win32serviceutil.ServiceFramework as I have seen in many examples on the Internet

I've tried it with os.startfile(), subprocess.call(), subprocess.Popen() and os.system() and always with the same result. The application's executable (that I launched from the service) is appearing as a subthread (subprocess) no like main process and you don't see any windows (GUI).

It doesn't matter if it's a program compiled by me or any executable like Windows Notepad.exe

def main(self):
    while self.isrunning:
        try:
            # Check if subprocess exists:
            s = subprocess.check_output('tasklist', shell=True)
            if bytes("notepad.exe", 'UTF-8') not in s:
                os.startfile('notepad.exe')

And I've also tried:

os.startfile('notepad.exe')
results = subprocess.Popen(['notepad.exe'], close_fds=True, creationflags=DETACHED_PROCESS)
subprocess.Popen('notepad.exe')

and in the other ways that I mentioned before. Same result.

The application runs like subprocess (the thread is running in the TaskManager Subprocess section) but no window is visible.

If I run the same code from a normal Python script it works perfect. The problem is doing it from the service.

Some help? Thanks in advance.

来源:https://stackoverflow.com/questions/57808197/how-can-i-run-an-exe-file-from-python-3-x-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!