Credit: Andy McKay
The win32all
package includes a
win32serviceutil
module that is specifically designed to
handle Windows services:
# needs win32all, or ActiveState's ActivePython distribution import win32serviceutil def service_running(service, machine): return win32serviceutil.QueryServiceStatus(service, machine)[1] == 4 def service_info(action, machine, service): running = service_running(service, machine) servnam = 'service (%s) on machine(%s)'%(service, machine) action = action.lower( ) if action == 'stop': if not running: print "Can't stop, %s not running"%servnam return 0 win32serviceutil.StopService(service, machine) running = service_running(service, machine) if running: print "Can't stop %s (???)"%servnam return 0 print '%s stopped successfully' % servnam elif action == 'start': if running: print "Can't start, %s already running"%servnam return 0 win32serviceutil.StartService(service, machine) running = service_running(service, machine) if not running: print "Can't start %s (???)"%servnam return 0 print '%s started successfully' % servnam elif action == 'restart': if not running: print "Can't restart, %s not running"%servnam return 0 win32serviceutil.RestartService(service, machine) running = service_running(service, machine) if not running: print "Can't restart %s (???)"%servnam return 0 print '%s restarted successfully' % servnam elif action == 'status': if running: print "%s is running" % servnam else: print "%s is not running" % servnam else: print "Unknown action (%s) requested on %s"%(action, servnam) if _ _name_ _ == '_ _main_ _': # Just some test code; change at will! machine = 'cr582427-a' service = 'Zope23' action = 'start' service_info(action, machine, service)
Mark Hammond’s win32all
package
makes it child’s play to code Python scripts for a
huge variety of Windows system-administration tasks. For example,
controlling Windows services becomes a snap. In addition to the few
features exemplified in this recipe, which are similar to those
provided by Windows’ own net
command, win32all
also gives you options such as
installing and removing services.
The functions this recipe uses from the
win32serviceutil
module are
StartService
,
StopService
, RestartService
,
and QueryServiceStatus
. Each takes two arguments:
the name of the service and the name of the machine. The first three
perform the start, stop, and restart as requested. The fourth returns
a structured code describing whether and how the given service is
running on the given machine, but in this recipe we exploit only the
fact, encapsulated in the recipe’s
service_running
function, that the second item of the return value is the integer 4
if and only if the service is running successfully.
Documentation for win32serviceutil
in
win32all
(http://starship.python.net/crew/mhammond/win32/Downloads.html)
or ActivePython (http://www.activestate.com/ActivePython/);
Windows API documentation available from Microsoft (http://msdn.microsoft.com); Python Programming on Win32, by Mark Hammond and Andy Robinson
(O’Reilly).