Windows Services

R

rodmc

Hello,

I am trying to figure out how to write a windows service, I am totally
new to it. I have written a simple test case, however it doesn't appear
to do anything. Although it installs and appears to start, when I run a
query of its status (using another app) it says it is not running. As
a note I am running it as the administrator in a Win2k machine to avoid
any problems with permissions - although I would rather not do this for
ever :)

I install the software as follows:

python testservice.py install
python testservice.py start

I am sure I am missing something out as I am basically working from
code samples for other solutions which I have seen. In particular one
for getting CherryPy to run as a service.

When I have figured this all out I intend to add in some code I have
already written which runs a server application.

It may be all rather obvious but I am quite new Python (just a few
months) and totally new to Windows services.

Thanks in advance for any help.

Best,

Rod


import win32serviceutil
import win32service
import win32event
import os,sys

class helloworld:

def test(self):
x=1
filename=open("c:test.txt","w")
while x<100000:
print >>filename,x
x=x+1

class MyService(win32serviceutil.ServiceFramework):
"""NT Service."""

_svc_name_ = "Test"
_svc_display_name_ = "Test"

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
# create an event that SvcDoRun can wait on and SvcStop
# can set.
self.stop_event = win32event.CreateEvent(None, 0, 0, None)

def SvcDoRun(self):
print "got here"
helloworld.test()
win32event.WaitForSingleObject(self.stop_event,
win32event.INFINITE)

def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.stop_event)

if __name__ == '__main__':
win32serviceutil.HandleCommandL
 
L

Larry Bates

rodmc said:
Hello,

I am trying to figure out how to write a windows service, I am totally
new to it. I have written a simple test case, however it doesn't appear
to do anything. Although it installs and appears to start, when I run a
query of its status (using another app) it says it is not running. As
a note I am running it as the administrator in a Win2k machine to avoid
any problems with permissions - although I would rather not do this for
ever :)

I install the software as follows:

python testservice.py install
python testservice.py start

I am sure I am missing something out as I am basically working from
code samples for other solutions which I have seen. In particular one
for getting CherryPy to run as a service.

When I have figured this all out I intend to add in some code I have
already written which runs a server application.

It may be all rather obvious but I am quite new Python (just a few
months) and totally new to Windows services.

Thanks in advance for any help.

Best,

Rod


import win32serviceutil
import win32service
import win32event
import os,sys

class helloworld:

def test(self):
x=1
filename=open("c:test.txt","w")
while x<100000:
print >>filename,x
x=x+1

class MyService(win32serviceutil.ServiceFramework):
"""NT Service."""

_svc_name_ = "Test"
_svc_display_name_ = "Test"

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
# create an event that SvcDoRun can wait on and SvcStop
# can set.
self.stop_event = win32event.CreateEvent(None, 0, 0, None)

def SvcDoRun(self):
print "got here"
helloworld.test()
win32event.WaitForSingleObject(self.stop_event,
win32event.INFINITE)

def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.stop_event)

if __name__ == '__main__':
win32serviceutil.HandleCommandL
I found Mark Hammond and Andy Robinson's book Python Programming on
Win32 to be a great resource when I wrote my first service. It has
several examples that helped a lot.

Remember services run inside of infinite loops. Inside SvcDoRun
you must have some construct like:

while 1:
#-----------------------------------------------------------------
# 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

else:
#
# Put body of service here
#



You also should probably change win32event.INFINITE to some number (expressed
in milliseconds) or your service gets blocked and doesn't do anything.

The sequence goes like this:

Inside an infinite loop, sleep for some time
Wake up and check if you received a stop signal
If you have, exit the infinite loop (by returning from SvcDoRun)
If not, run whatever code you want to execute each time you loop

Hope this helps.
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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top