The following code snippet shows you have to create a windows service from a python script. The most important thing here is the username and password, if you ignore supplying these the server will never start and you will get a message saying that the service has not responded in time, this is a red herring.
Jun 24 2008onUsing Python to Manage Windows ServicesI recently ran into a little problem with a Windows service.Apache Tomcat, about once every two weeks or so, simply croaks on one of our servers. When I say croaks, I mean croaks: a hit to:8080 gives a fast no-server-listening-whatsoever message.Tomcat logs lead me to belive it’s a java memory issue, but as that service is moving to a new virtual server soon, I don’t want to get too deep in it yet in case it just goes away after the move (lazy smart). That doesn’t mean I want to spend a lot of time periodically checking on it, however.There are any number of great enterprise monitoring tools for this sort of thing, such as and, which are for the most part free and open source. That would have been like squirrel hunting with a bazooka in this case, however, and both of the aforementioned products wouldn’t jive well with our Linux-phobic IT department. But a little Python can easily do the trick.First you need to install the, which adds a lot of neat functions, including what we’re looking for - win32serviceutil.
Now we build a simple function to stop, start, restart, or query the status of a Windows service.def servicemanager(action, machine, service):if action ‘stop’:win32serviceutil.StopService(service, machine)elif action ‘start’:win32serviceutil.StartService(service, machine)elif action ‘restart’:win32serviceutil.RestartService(service, machine)elif action ‘status’:if win32serviceutil.QueryServiceStatus(service, machine)1 4:print “%s is happy”% serviceelse:print “%s is being a PITA”% serviceNow we’re off and running. Since the problem here is Tomcat just up and dies, a simple urlopen to Apache will do it.import urllibimport socketimport win32serviceutildef serviceinfo(action, machine, service):socket.setdefaulttimeout(30)try:f = urllib.urlopen(“)print “Tomcat is smokin’.”except:print “Tomcat is dead. Restarting the service.”servicemanager(“restart”, “servername”, “Apache Tomcat”)This little bit of code sets the default connection timeout to 30 seconds, so if the service is up and running, it will spend up to 30 seconds loading the URL to f. If that runs without error, we’re golden. If a socket error occurs (i.e. Tomcat is not talking), the exept block is executed, restarting the Tomcat service.
Set the tiny script to run every 10 minutes or so, Bob’s your uncle, a self-healing server.You can do quite a few service management tricks with this simple code. Say you’ve got an ArcIMS server that’s croaking on you (not an uncommon thing). Wouldn’t it be nice if the services would simply restart themselves?Let’s say you have a map service named mapservice on a server named servername (we’re also implying you’re incredibly uncreative here). ArcIMS is making images that look like so:mapserviceSERVERNAME5.xxxIf ArcIMS has taken a walk, a hit to a web page with the map on it won’t have a link to your map image.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |