Tag Archives: task scheduler

Creating Windows Services in Python

If you’ve ever been tasked with automating processes in MS Windows that run in the background or at night, you’ve likely worked with the Windows Task Scheduler (not to be confused with the Task Manager) and then some form of batch file, power shell scripts, or similar. If you were lucky enough to have Python installed on the server (assuming you are working on a server), you’ve probably run something like “python.exe mypythonscript.py” either directly or through a *.bat or *.cmd file. There are lots of tutorials online for this kind of thing.

But you might find that you want more control over the sequence of actions that make up your task, you might like some conditional controls, or have one action generate some output before another action starts. In short, you’ll enjoy being able to code all this in Python. Soon, you’ll start wonder if you can replace all your scheduling with a Python services that runs all the time and only performs certain actions, such as checking for the existence for a file, when a certain trigger (time of day) goes off. So how to write your Python service?

Two good posts explaining how to create a Windows service written in Python are this one (ryrobes.com) and this one (chrisumbel.com). Either way, you will be using Mark Hammond’s Win32 extension, now available through Sourceforge. If you’re like me, you might’ve picked up a used copy of “Python – Programming on Win32” (Mark Hammond & Andy Robinson). Granted it’s quite dated (Jan 2000) but it’s a good reason for all things Python on Windows and has a whole chapter on “Windows NT services” that is the basis for what the links above illustrate.

There is a post on debugging Python win services here. If you use or are familiar with Active Python, then there are alternatives such as PythonCom, an example for which you can find here on stackoverflow. Finally, here is a recipe on how to do this using Windows Server 2003 Resource Kit Tools (RKT). If you’re like me and you’re jumping back and forth between different version of Windows Server, that may be useful.

Ok, this was a collection of useful links. Next post will have some code again.

Advertisements

Leave a comment

Filed under Python