title: Starting Application as a service before logon using python
body: <p>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 <code>SMWinservice</code> is defined <a href="https://www.thepythoncorner.com/2018/08/how-to-create-a-windows-service-in-python/?doing_wp_cron=1561897401.6327629089355468750000" rel="nofollow noreferrer">here</a>.</p>
  
  <pre class="lang-py prettyprint-override"><code># 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()
  </code></pre>
  
  <p>The above code works fine when I simply try to write something in a file (after looking at <a href="https://stackoverflow.com/questions/41200068/python-windows-service-error-starting-service-the-service-did-not-respond-to-t">this post</a> and solving errors).</p>
  
  <p>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.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I changes the <code>main</code> method to the following.</p>
  
  <pre class="lang-py prettyprint-override"><code>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()
  </code></pre>
  
  <p>When I start the process, I can see the process pid being updated in <code>C:\logs.txt</code>. 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?</p>