My Flask service stops because I logged out in Windows. When I reload my username and password, it works again. How can I make it always on, even when I reboot or log out?
import time
import random
import os
from pathlib import Path
from SMWinservice import SMWinservice
import sys
import win32serviceutil
import win32service
import win32event
import servicemanager
class FlaskService(SMWinservice):
_svc_name_ = "FlaskServiceSmartFactory01"
_svc_display_name_ = "Flask Service Smart Factory01"
_svc_description_ = "Python service framework Smart Factory01"
def start(self):
self.isrunning = True
def stop(self):
self.isrunning = False
def main(self):
os.chdir(os.environ['SMARTFACTORYPATH'])
os.system('C02_RestAPI.py')
if __name__ == '__main__':
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(FlaskService)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(FlaskService)
Windows services can run in background when Windows is running but you can't run it if Windows is off. You can set this service to be load at Windows start if you want.