Run Python script in the background or as a service


Hi, I have programmed a HIDS (Host Intrusion Detection System) which is in , I need to run this as a WINDOWS service or in the background permanently WITHOUT BEING ACCESSED TO THE CODE OF .

I use Python 3.6 and I tried to create a WINDOWS service as indicated here: link , however, it seems to be that the import do not work to me even having installed the requirements, since I believe that in Python 3.6 the name of import have changed and I do not manage to make it work.

Any way to run permanently in the background without being able to see the source code of ?

asked by Jota 09.02.2017 в 21:19

2 answers


Hello good morning, I'll add an example of a service in python. link

import win32serviceutil, win32service
import win32event, win32api
import servicemanager
import time

import win32gui, win32gui_struct, win32con

class EventDemoService(win32serviceutil.ServiceFramework):
    _svc_name_ = "PyServiceEventDemo"
    _svc_display_name_ = "Python Service Event Demo"
    _svc_description_ = "Demonstrates a Python service which takes 
    advantage of the extra notifications"
    def __init__(self, args):
         win32serviceutil.ServiceFramework.__init__(self, args)
         self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
         self.running = True
    def SvcStop(self):
         self.running = False
    def SvcDoRun(self):
         while self.running:
         servicemanager.LogInfoMsg("aservice - is alive and well")
def ctrlHandler(ctrlType):
    return True

if __name__=='__main__':
    win32api.SetConsoleCtrlHandler(ctrlHandler, True)
answered by 22.07.2017 в 00:47

First you have to create an executable, for example with link and then you can create the service with sc.exe create binPath="

answered by 09.02.2017 в 22:37