[python-win32] Using signal handlers in a Windows Service
Justin Johnson
justinjohnson at gmail.com
Mon Dec 5 18:19:38 CET 2005
I'm trying to setup an application that runs as a Windows service to rotate
logs upon receiving a certain signal. Whenever I try to do this I get the
following error.
Exception in TestService.SvcStart: signal only works in main thread
Attached is a very trimmed down example. To run the example and see the
error message, do the following.
1. In one DOS window run
PYTHONHOME\Lib\site-packages\win32\lib\win32traceutil.py to begin collecting
trace output.
2. In a separate DOS window run "python tsvc.py --username domain\user
--password yourpassword --startup auto install" using your own user and
password.
3. Using Start > Control Panel > Administrative Tools > Services,
modify the service properties, modifying the user and password again (even
though you already specified them) and click OK. Accept any prompts about
granting the appropriate rights to run as a service.
4. Run "net start tsvc"
5. Observe the error in the win32traceutil window.
I realize this is a known problem but am unsure how to get around it to make
sure my log rotation works when receiving the signal. Does anyone have any
ideas on how to get around this limitation?
Thanks.
Justin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20051205/d48aca3f/attachment.html
-------------- next part --------------
import win32serviceutil
import win32service
import sys
import os
SVC_NAME = "tsvc"
SVC_DISPLAY_NAME = "Test Service"
class TestService(win32serviceutil.ServiceFramework):
_svc_name_ = SVC_NAME
_svc_display_name_ = SVC_DISPLAY_NAME
def SvcDoRun(self):
## Import inside the method, so we redirect all output
## to the trace output collector. If you need to debug this service,
## run win32traceutil.py as a script. It will receive all output from
## this service.
import win32traceutil
try:
import signal
SIGUSR1 = 10 # Windows doesn't care what the number is
def rotateLog(signal, frame):
pass
signal.signal(SIGUSR1, rotateLog)
except Exception, e:
print "Exception in TestService.SvcStart:", str(e)
def SvcStop(self):
## See above for why we import this.
import win32traceutil
try:
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
except Exception, e:
print "Exception in TestService.SvcStop:", str(e)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(TestService)
More information about the Python-win32
mailing list