Run Python script in the background or as a service

1

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

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 fichero.py permanently in the background without being able to see the source code of fichero.py ?

    
asked by Jota 09.02.2017 в 21:19
source

2 answers

2

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.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
         win32event.SetEvent(self.hWaitStop)
         self.running = False
    def SvcDoRun(self):
         self.ReportServiceStatus(win32service.SERVICE_RUNNING)
         while self.running:
         servicemanager.LogInfoMsg("aservice - is alive and well")
         time.sleep(3)
def ctrlHandler(ctrlType):
    return True

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

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