Quality Web Site and Ecommerce Hosting site map

Why BigBlueHost?

  • Fast, Free 24/7 Support
  • Money Back Guarantee
  • Company Hosting
  • Affordable Webhosting
  • Web Host Control Panel
  • 99% Server Uptime
  • Plan Flexibility
  • 100% Dependable
  • Security Features

BigBlueHost © 2003-2006

Internals: Replacing Task Scheduler with Cron

This article will discuss using a Cron type system, as used on Unix and Linux systems, to bring the flexibility, scalability and a need for more out of a task automation tool, to the Win32 environment.

Internals: Replacing Task Scheduler with Cron

  • As our dependency upon machines for various tasks grow, time becomes an integral factor that may work against us, when pressed upon deadlines. Automation of such tasks, without the need for human intervention, becomes vital to whether one is able to square away enough time to complete more and more tasks with little help from human hands.

    Task Scheduler, which comes bundled with Windows attempts to make automation of tasks effortless. Unfortunately, it is not very configurable and basic in what it is capable of.

    On Unix and Linux systems, Cron is what is used for task scheduling. This scheduler is very configurable, and is capable of well more then its Windows counterpart.

    This article will discuss using a Cron type system, as used on Unix and Linux systems, to bring the flexibility, scalability and a need for more out of a task automation tool, to the Win32 environment.

    Tools:

    Pycron from Kalab.com:
    Pycron from kalab.com

    Install the product and make sure to install it as a service (default option on the last dialog box of the installer) if your Win32 operating system supports this.

    Then click Start->Run->services.msc (hit enter)

    Scroll down to and highlight Task Scheduler->right click->Properties->Toggle to Manual->hit Stop->then Apply->OK

    Then scroll up to Python Cron Service->highlight->right click->Properties->Toggle to Automatic->Apply->OK

    We will be working with a file called crontab.txt for all of the scheduled entries. This file must be created in the Pycron Program directory which is located in the pycron folder under the Program Files folder.

    Create a new file called crontab.txt in the Pycron Program Directory and put this in to it

    * * * * * replace replace
    Save your file.

    Now launch the crontab editor (Start->Programs->Pycron->Pycron CronTab Editor)

    By default it will load up the contents of the crontab.txt file in the Pycron Program Directory.

    Screenshot

    The parameters of a crontab entry from left to right are as follows.

    0-59 - Minute
    0-23 - Hour
    1-31 - Day
    1-12 - Month
    0-6 - Day of the week (0 For Mon, and 6 for Sunday)

    Command/Application to execute

    Parameter to the application/command.

    entry:
    Minute Hour Day Month Day_of_the_week Command Parameter

    Note:
    * is a wildcard and matches all values
    / is every (ex: every 10 minutes = */10) (new in this Win32 version)
    , is execute each value (ex: every 10 minutes = 0,10,20,30,40,50)
    - is to execute each value in the range (ex: from 1st (:01) to 10th (:10) minute = 1-10)

    Double click the 1st entry of “ * * * * replace replace” to edit the entry.

    Screenshot

    For an example, we will run defrag on every Friday at 11:00 (23:00) PM against the C:\ volume.

    On the Command line hit Browse, and navigate to your System32 Folder inside your Windows folder and double click on defrag.exe

    On the Parameter line enter in c:

    Always run a Test Execution to make sure your command is valid. If all was successful, you will see your command/application run and a kick back message of Successful will be observed.

    For Minute, erase the * and enter in 0
    For Hour, erase the * and enter in 23
    For Week Day enter in 4.

    Then hit OK, File->Save.

    Note: You can use the wizard to enter in values for each entry as well.

    Now open up a command prompt (start->run->cmd), and type:
    net start pycron

    You can leave it running now, and every time you append and or change your crontab.txt, the schedule will be updated.

    To add another entry using the crontab GUI, add in a * * * * * replace replace to crontab.txt on the next free line, save it, then open up crontab.txt with the GUI editor and make the desired changes on the entry by double clicking to edit.

    It is recommended that every time the GUI is used to edit entries, that you observe the entry in crontab. After you become comfortable with the syntax of cron entries, there will be no need for the GUI editor.

    The entry for our defrag command becomes:
    0 23 * * 4 “C:\WINDOWS\System32\defrag.exe” c:

    This same task can be performed with the Task Scheduler with one entry.

    Let us go through another example of more complexity, which would not as easily be accomplished with the Task Scheduler.

    I want to back up my work files every 3 hours, from Monday through Friday, between the hours of 9AM to 6PM, for all the months of the year. The work folder is C:\WORK and the backup folder is C:\BACKUP.

    Open up crontab.txt and on the next free line, enter in * * * * * replace replace, then save it.

    Open up the crontab editor and import crontab.txt. Double click the “* * * * replace replace” entry.

    For Command, browse to xcopy located in System32 within your Windows folder.

    For Parameter: C:\WORK\* C:\BACKUP /Y

    For Minute: 0
    For Hour: 9-18/3
    For Day: *
    For Month: *
    For Week Day: 0-4

    Click OK->File->Save.

    The entry for this task as reflected in our crontab.txt becomes
    0 9-18/3 * * 0-4 “C:\WINDOWS\System32\xcopy.exe” C:\WORK\* C:\BACKUP /Y

    If we were to schedule the above example with the Task Scheduler that comes with windows, then a separate entry for every 3rd hour mark in terms of time (AM/PM) between the aforementioned times would have to be entered, for the task.

    Note: Cron can work with your own written batch/script files as well.

    Note: You can view other examples in crontab.sample, located in the pycron program directory.

    As you can see, Cron has a lot more to offer then the Task Scheduler. Not to say that the Windows application is not useable, but for those scenarios where you need the ability to be flexible, and configurable without all the extra headaches, then this is the ideal replacement for you. It proves to be much more efficient as well in practice.

    Further Reading:

    Cron Help Guide at linuxhelp.net

    Pycron Home

    Author: Pershoot (pershoot @ bigbluehost . com)