By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,574 Members | 2,966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,574 IT Pros & Developers. It's quick & easy.

Stopping Windows Service

P: n/a
D
I have a simple file server utility that I wish to configure as a
Windows service - using the examples of the Python Win32 book, I
configured a class for the service, along with the main class functions
__init__, SvcStop, and SvcDoRun (which contains my server code). After
registering the service, I am able to start it with no problems.
However, it never stops correctly (net stop returns "service could not
be stopped") and service is left in a Stopping state. My gut tells me
there's something additional I need to include in the SvcDoRun function
in order to have it shutdown gracefully, but I'm not sure what. Also,
if that is the case, where should I place the statements within the
function (i.e. right after the listen() statement?)? Thanks as
always..

Doug

Mar 1 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Wed, 2006-03-01 at 12:41 -0800, D wrote:
I have a simple file server utility that I wish to configure as a
Windows service - using the examples of the Python Win32 book, I
configured a class for the service, along with the main class functions
__init__, SvcStop, and SvcDoRun (which contains my server code). After
registering the service, I am able to start it with no problems.
However, it never stops correctly (net stop returns "service could not
be stopped") and service is left in a Stopping state. My gut tells me
there's something additional I need to include in the SvcDoRun function
in order to have it shutdown gracefully, but I'm not sure what. Also,
if that is the case, where should I place the statements within the
function (i.e. right after the listen() statement?)? Thanks as
always..

At the risk of sounding like a geek, first problem I see is WIndows:)
Sorry couldn't help that one:) Seriously though, I would suggest that it
sounds like something is not letting the process go, which would be par
for the windows course. If you will post the snippet of code, I'm sure
we could help you a little more specifically.

Mar 1 '06 #2

P: n/a
D wrote:
I have a simple file server utility that I wish to configure as a
Windows service - using the examples of the Python Win32 book, I
configured a class for the service, along with the main class functions
__init__, SvcStop, and SvcDoRun (which contains my server code). After
registering the service, I am able to start it with no problems.
However, it never stops correctly (net stop returns "service could not
be stopped") and service is left in a Stopping state. My gut tells me
there's something additional I need to include in the SvcDoRun function
in order to have it shutdown gracefully, but I'm not sure what. Also,
if that is the case, where should I place the statements within the
function (i.e. right after the listen() statement?)? Thanks as
always..

Doug

Stopping is done by having SvcStop set the event flag so that the
next time through your loop in SvcDoRun you see that the stop has
been issued and exit your while loop. Here is some skeleton code
that I stripped from a working service. Hope it helps. Note: I
copied most of this from Mark Hammond and Andy Robinson's excellent
book Python Programming on Win32 book.

-Larry Bates

import win32serviceutil
import win32service
import win32event
import win32evtlogutil

class testService(win32serviceutil.ServiceFramework):

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
#---------------------------------------------------------------------
# Create an event which we will use to wait on.
# The "service stop" request will set this event.
#---------------------------------------------------------------------
self.hWaitStop=win32event.CreateEvent(None, 0, 0, None)
return

def SvcStop(self):
#---------------------------------------------------------------------
# Before we do anything, tell SCM we are starting the stop process.
#---------------------------------------------------------------------
self.ReportServiceStatus(win32service.SERVICE_STOP _PENDING)
#---------------------------------------------------------------------
# And set my event
#---------------------------------------------------------------------
win32event.SetEvent(self.hWaitStop)
return

def SvcDoRun(self):
import servicemanager
#---------------------------------------------------------------------
# Make entry in the event log that this service started
#---------------------------------------------------------------------
servicemanager.LogMsg(servicemanager.EVENTLOG_INFO RMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))

#---------------------------------------------------------------------
# Wait 10000 milliseconds (10 seconds)
#---------------------------------------------------------------------
self.timeout=10000
#-----------------------------------------------------------------
# Wait for service stop signal, if I timeout, loop again
#-----------------------------------------------------------------
rc=win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
#
# Check to see if self.hWaitStop happened
#
if rc == win32event.WAIT_OBJECT_0:
#
# Stop signal encountered
#
break

#--End while--
#---------------------------------------------------------------------
# Log stopped message to EventLog
#---------------------------------------------------------------------
servicemanager.LogMsg(servicemanager.EVENTLOG_INFO RMATION_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(self._svc_name_, ''))

return
Mar 1 '06 #3

P: n/a
D
Thanks guys. Larry - I inserted the rc lines right after I do the
listen() and the service is not functioning properly (service is
terminating). Where am I going wrong here?

Doug

Mar 1 '06 #4

P: n/a
D
I must be missing something..in my SvcDoRun function, first line is
"self.timeout=10000" , followed by my "while 1:" loop which listens for
the connection, processes the data, etc. Where should I place the
"rc=win32event.." and "if rc" lines? I have a feeling I'm pretty close
here.. :) Thanks very much.

Doug

Mar 2 '06 #5

P: n/a
D
Sorry for the many messages, but I've been looking into it further -
since I have the server services listening and waiting for incoming
connections, how would I interrupt it to let the program know that a
stop request has been issued? For example, if I put the stop check
before it listens, I would think it'd have to wait until it loops back
around after the next client connects. If I put it after the listen, a
client would have to connect in order for it to run the check..?

Mar 2 '06 #6

P: n/a
D wrote:
Sorry for the many messages, but I've been looking into it further -
since I have the server services listening and waiting for incoming
connections, how would I interrupt it to let the program know that a
stop request has been issued? For example, if I put the stop check
before it listens, I would think it'd have to wait until it loops back
around after the next client connects. If I put it after the listen, a
client would have to connect in order for it to run the check..?

You are correct. Services must be "good neighbors". They
do some work and go to sleep for some period of time, then wake
up and loop. The stop signal comes while they are asleep.
When they wake up, they exit the infinite loop. If you can't
implement a listen-with-timeout it is going to be hard to make
this into a service.

-Larry Bates
Mar 3 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.