简体   繁体   中英

Starting Application as a service before logon using python

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?

这是由于限制服务无法与 Windows Vista 时代的桌面交互。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM