Stopping Windows Service

P

Paul Metzger

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.
 
D

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
 
L

Larry Bates

D said:
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_INFORMATION_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_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(self._svc_name_, ''))

return
 
D

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
 
D

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
 
D

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..?
 
L

Larry Bates

D said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top