I am trying to create a small service, for a Windows 2012 R2 server, on python and I ran into some problems. The basis for the skeleton is a pattern that is very common in the internet.

# -*- coding: utf-8 -*- import win32serviceutil import win32service import win32event import servicemanager import socket import threading import subprocess import os import sys import time class AppServerSvc(win32serviceutil.ServiceFramework): _svc_name_ = "tstsrvc" _svc_display_name_ = "Test Service" def __init__(self,args): win32serviceutil.ServiceFramework.__init__(self,args) self.hWaitStop = win32event.CreateEvent(None,0,0,None) socket.setdefaulttimeout(60) def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) win32event.SetEvent(self.hWaitStop) def SvcDoRun(self): self.ReportServiceStatus(win32service.SERVICE_START_PENDING) servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_,'')) self.main() win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE) def main(self): sock = socket.socket() sock.bind((socket.gethostbyname(socket.gethostname()), 9666)) sock.listen(1) def action(client_socket): client_socket.send('Hello ! You are connected to {0}'.format(socket.gethostbyname(socket.gethostname()).encode()) client_socket.close() while True: conn, addr = sock.accept() data = conn.recv(1024) if data: threading.Thread(target=lambda: action(conn), daemon=True).start() if __name__ == '__main__': if len(sys.argv) == 1: servicemanager.Initialize() servicemanager.PrepareToHostSingle(AppServerSvc) servicemanager.StartServiceCtrlDispatcher() else: win32serviceutil.HandleCommandLine(AppServerSvc) 

I collect this script in the exe file and try to put it on the server.

 sc create tstsrvс start= auto type= own DisplayName= "Test Service" binPath= "%systemroot%\tstsrvc.exe" >>>[SC] CreateService: успех 

The service is installed, I run it and it works. But after a reboot, the service starts, runs for a couple of seconds and turns off. What could be the reason ?

By itself, the server is terminal. And as I understand it, you need to install the service with the " share " type, but when I do this, when I start the service, I get an error:

 "Ошибка 1083: Исполняемая программа, запуск которой настроен в службе, не включена в состав службы." 

Python is not installed on the server, so I collect the service in .exe .

  • Does this exe have dependencies? - VTT
  • @VTT, no. At the moment after the reboot, if you turn to it, it will work, but it will still turn off later. - Acamori

0