Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4102540
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:50:25+00:00 2026-05-20T20:50:25+00:00

Below is the framework for the windows service that I am running. In the

  • 0

Below is the framework for the windows service that I am running. In the event of an error, the error is logged and can be viewed in the event viewer. The problem is that the script just quits and does not restart again even though I have the recovery set to restart the service on the first, second and subsequent failures. Currently I have little error handling because I want to see what errors that may arise in the event viewer so that I can write code to handle these errors accordingly.

from win32api import CloseHandle, GetLastError, SetConsoleCtrlHandler
import os
import sys 
import time
import pythoncom
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket

class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "my_service_name"
    _svc_display_name_ = "my service"

    def __init__(self,args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        SetConsoleCtrlHandler(lambda x: True, True)
        self.hWaitStop = win32event.CreateEvent(None,0,0,None)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)
        self.run = False

    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_,''))
        self.run = True
        self.main()

    def main(self):
        while self.run == True
            pass          

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(AppServerSvc)

Edit:

I tried to try: except around self.main but the outcome was still the same. The service did not restart when it crashed… Please anyone out there with any ideas? A service is not all that useful if it cannot restart in the event of a crash… Might as well run it as a .pyc

Edit:

below is an example of an error that may arise within my script… I don’t believe this error message is particularly useful because what I am try to achieve is to have the service restart but none the less here is an example of an error that has crashed my service without it restarting:

The instance's SvcRun() method failed 
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\win32\lib\win32serviceutil.py", line 806, in SvcRun
    self.SvcDoRun()
  File "C:\Some_Service.py", line 46, in SvcDoRun
    self.main()
  File "Some_Service.py", line 61, in main
    ser = self.open_serial_port()
  File "Some_Service.py", line 70, in open_serial_port
    serial_connection.open()
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 56, in open
    raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.WinError()))
SerialException: could not open port COM6: [Error 1225] The remote system refused the network connection. 
%2: %3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:50:26+00:00Added an answer on May 20, 2026 at 8:50 pm

    Below is a service that just divides by zero to raise an error. If there is an error, an event is sent and the service exits using os._exit(-1). The value has to be anything but 0 so that the windows knows that the service didn’t exit nicely.

    from win32api import CloseHandle, GetLastError, SetConsoleCtrlHandler
    import os
    import sys
    import time
    
    import win32serviceutil
    import win32service
    import win32event
    import servicemanager
    
    import traceback
    
    
    
    class AppServerSvc (win32serviceutil.ServiceFramework):
        _svc_name_ = "test"
        _svc_display_name_ = "test"
    
        def __init__(self,args):
            win32serviceutil.ServiceFramework.__init__(self,args)
            SetConsoleCtrlHandler(lambda x: True, True)
            self.hWaitStop = win32event.CreateEvent(None,0,0,None)
    
    
    
    
        def SvcStop(self):
            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
            win32event.SetEvent(self.hWaitStop)
            self.run = False
        def SvcDoRun(self):
    
            servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                                  servicemanager.PYS_SERVICE_STARTED,
                                  (self._svc_name_,''))
            self.run = True
            try: # try main
                self.main()
            except:
                servicemanager.LogErrorMsg(traceback.format_exc()) # if error print it to event log
                os._exit(-1)#return some value other than 0 to os so that service knows to restart
    
    
        def main(self):
    
            while self.run == True:
                time.sleep(30)         
                t = 1/0
    
    if __name__ == '__main__':
        win32serviceutil.HandleCommandLine(AppServerSvc)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am using asp.net (.net framework 2.0) i am getting below error for my
I have a little problem with memory management in a Windows Service written in
I am facing a peculiar problem related to a hosted WCF service (.NET Framework
Hi All I get below error when sending request to OSR (Oracle Service Registry)
I have a windows service that I need to migrate to onto Azure as
We are running Crystal Reports on a Windows Server 2008 with .NET framework 3.5
At first I should mention that this problem only occurs in windows forms applications
I can run this fine: $msbuild = C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe start-process $msbuild -wait But when I
I have a Zend Framework application structure as below: /application /library /Zend /Core /Filter
I tried to install MVC Framework from the below URL http://www.asp.net/mVC/ First it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.