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 6761325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:10:27+00:00 2026-05-26T14:10:27+00:00

I have used this question to learn install my python script as a service

  • 0

I have used this question to learn install my python script as a service with ActivePython. I am able to install my script and remove it, as I would expect, but my problem is that once I start the script running, I then lose the ability to stop it or remove it. It just runs permanently and I cannot get rid of it. The answer to the linked question mentions checking a flag to stop the script. Can someone explain how to do this?

Right now the script does very little. just prints lines to a file every few seconds. I want to get it working before I move on to more complicated things.

import pythoncom
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import time


class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "PythonTest"
    _svc_display_name_ = "Python Test"
    _svc_description_ = "This is a test of installing Python services"

    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):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                          servicemanager.PYS_SERVICE_STARTED,
                          (self._svc_name_,''))
        self.main()

    def main(self):
        i=0
        while True:
            f=open("C:\\hello.txt","a")
            f.write("hello"+str(i)+"\n")
            f.close()
            i=i+1
            time.sleep(5)


if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(AppServerSvc)
  • 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-26T14:10:28+00:00Added an answer on May 26, 2026 at 2:10 pm

    Of course you can’t stop it, you’re stuck in an infinite loop. Replace:

    time.sleep(5)
    

    with this:

    if win32event.WaitForSingleObject(self.hWaitStop, 5000) == win32event.WAIT_OBJECT_0: 
        break
    

    Should do the trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used the mapping solution from this question to have a joined component. But
I have used this code for extracting urls from web page.But in the line
I have used sessions before but never cookies. I would like to use cookies
I've never used TDD and unit testing properly, but would like to learn some
I have a more general question but any advice in implementing this in C#
I have used this in my HTML: <q> Hai How r u </q> Which
I have used this effect before, everything is in order (As far as I
I am creating a rails app and have used this code in one of
I have used code like this: http://msdn.microsoft.com/en-us/library/dw70f090.aspx to access database before when working in
i have used impersonation in this application. whenever this error occurs i required to

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.