In Windows, there is the graphical tool Task Scheduler, which allows to, well, schedule tasks. It’s nice, but it’s not scriptable. So we go for the command line here.
First, we write a small Python program, which represents the actual work to do. In this case, it writes a live log, i.e. each time it runs, it adds a line of text to a file. This file could be monitored, e.g. to check if a server machine is up and running. Of course, its functionality could be extended.
livelog = open("live.log", "at")
message = time.strftime("Rufus is alive at %Y-%m-%dT%H:%M:%S\n", time.localtime())
In order to execute the Python program, we create a Windows batch file. This allows us to control the environment under which the Python program runs. Gives flexibility, at least.
In order to gain more flexibility, we can add another layer of batch files, which allows us to split up tasks.
But now to some serious scheduling. In Windows, the command line program with the lovely name
schtasks is responsible for creating, querying, and deleting tasks, and does even more. A simple call to this program (from another batch file, for example) creates a livelog that is written every minute:
schtasks /Create /SC MINUTE /TN Livelog /TR c:\sw\tutorial\dojob.bat
You can schedule the tasks in a variety of manners. For example, a job can be run twice every week, at 4 o’clock in the morning, the best time for some action:
schtasks /Create /SC WEEKLY /D TUE,SAT /ST 04:00 /TN PerformAction /TR c:\sw\tutorial\dojob.bat
You can specify complex schedules, and there is a little bit of syntax to master. Consult the help:
schtasks /Create /?, which is quite good. Creating tasks is fine, and now we would like to see if they are actually running.
schtasks will tell you whether a task of yours is active and when it will run the next time:
schtasks /Query /TN Livelog
schtasks /Query /TN PerformAction
If you get tired of a task, just delete it:
schtasks /Delete /TN PerformAction