Windows cron?

As anyone who has been in the Database Administrator business for some time knows, most Unix-type operating systems come with a robust but sometimes cryptic utility called cron.

No, but close

Crom, I mean cron is used to schedule the automatic execution of scripts or programs on the Unix/Linux server.  It is often used to schedule backups or perform maintenance for Oracle databases created on the server.  Combining this utility with the flexibility of shell scripts is great for shops where a centralized management utility is absent.

If you are looking at Windows for a similar cron/script combination like Unix/Linux, then the Task Scheduler and basic batch files are available.  You can also use Powershell scripts for both Windows and Unix/Linux environments, but we will concentrate on a simple batch file example this time.

To open the Task Scheduler utility, press and hold the Windows key, then press the “s” key to open the Windows Search utility.

Windows button, in case you did not know

Once the search utility is open, start typing the word task and the Task Manager and Task Scheduler utilities should be listed.  Click on Task Scheduler and the utility appears.

We are going to pause here and create two simple batch files for stopping/disabling and enabling/starting a Windows service.  The purpose is to stop this service over a weekend and disable it at the same time so that a server reboot does not automatically start the service before we want it started.  To create these batch files, open a text file editor such as Notepad, copy and paste the lines below, and save the files as stop.bat and start.bat

Contents of the stop.bat batch file:

@echo off
net stop PrintService
sc config PrintService start=disabled
exit

Contents of the start.bat batch file:

@echo off
sc config PrintService start=auto
net start PrintService
exit

Now that we have our batch files created, it is time to schedule them for execution.  Go back to the Task Scheduler, pull down the Action menu at the top, and select Create Task.

In this General screen, give the task an informative name and a short description of what the task will do.

It is important to note the Security options here.  By default, this new task will execute as the account you are running the Task Scheduler under.  You can select a different account, but you MUST have the credentials for this account, especially if you want to automate the task to execute even if that account is not currently connected by selecting the “Run whether user is logged on or not” radio button.

Use caution when an actual user account is selected, as the task will fail if that account is disabled for some reason such as employee departure.  If this task is something that will execute for some time to come, consider creating a local account on the Windows server to own the task.

Whichever option you choose, make sure the account that owns the task has sufficient privileges to execute the commands in the batch files.  In this case, administrator privileges are required to manipulate Windows services.

After completing the information in this screen, go to the Triggers tab and click the New button.

This is where we will schedule the task to execute.  You have one time, daily, weekly, and monthly options.  The rest of this screen is fairly self-explanatory.  At the bottom of the screen is an Enabled option that will allow you to schedule the task, but not have it enabled to run yet.

After completing the information in this screen, go to the Actions tab and click the New button.

Here you will designate the action this new task will perform.  In later versions of this utility, only the “Start a program” action is available in the Action drop-down list, which is fine for our example.

Browse to the location of the stop.bat file and select it.  Click OK once the batch file is selected.  You can review the settings under the Conditions and Settings tabs, but for our example, the default settings are sufficient.

Click the OK button in the Create Task window once you are done.  If you selected to have this new task execute whether the executing account is logged on or not, you will be prompted for the password for this account, which the Task Scheduler will store.  If the password changes for this account, the tasks associated with the account will fail until the stored passwords are updated in the tasks.

Repeat this process to create a task for the start.bat batch file and you are all set.

Enjoy.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s