I want to start an exe before login and I am trying to achieve this using Python. Below is a sample code that I am trying to use where SMWinservice
is defined here.
# imports are there
class MyService(SMWinservice):
_svc_name_ = 'testService1'
_svc_display_name_ = 'Test Service 1'
_svc_description_ = 'Test ServiceFramework 1 Description'
def start(self):
self.isrunning = True
def stop(self):
self.isrunning = False
def main(self):
subprocess.Popen(r'C:\Program Files\Notepad++\notepad++.exe')
# i=0
# while self.isrunning:
# with open('C:\\test1323.txt','a') as file:
# file.write(datetime.now().isoformat()+'\n')
# i=i+1
# time.sleep(5)
if __name__ == '__main__':
MyService.parse_command_line()
The above code works fine when I simply try to write something in a file (after looking at this post and solving errors).
But the exe doesn't start up when I try to start the service(manually or by restating the PC) and I don't understand why. What am I missing or what is wrong? How should I approach such a problem. I am new to windows functionality through the eyes of python. I have stuck in this for days and any code or help is much appreciated.
EDIT
I changes the main
method to the following.
def main()
self.process = subprocess.Popen('C:\\Program Files\\Notepad++\\notepad++.exe')
while self.isrunning:
if self.process.pid not in psutil.pids():
self.isrunning = False
with open('C:\\logs.txt','a') as file:
file.write(str(self.process.pid)+'\n')
time.sleep(5)
self.process.kill()
When I start the process, I can see the process pid being updated in C:\logs.txt
. The process can also be seen running in Task Manager as notepad++.exe running. But the application is not there in the foreground. Any thoughts?