Recipe5.7.Scheduling a Task


Recipe 5.7. Scheduling a Task

Problem

You want to schedule a task to run at a certain time or periodically.

Solution

Using a graphical user interface

  1. From the Control Panel, open the Scheduled Tasks applet.

  2. Double-click Add Scheduled Task.

  3. Click Next.

  4. Select the program you want to schedule to run.

  5. Type a name for the task, select the frequency in which to run it, and click Next.

  6. Enter the username and password of the user the task should run as and click Next.

  7. If you want to go back and modify any of the settings for the task, check the box beside Open advanced properties and click Finish.

Using a command-line interface

On Windows Server 2003 or Windows XP, you can use the schtasks.exe command to schedule a task. The following command creates a task to run weekly at 1:00 AM:

> schtasks /create /SC WEEKLY /TN "Disk Space Checker" /TR "c:\perl\bin\perl.exe  c:\scripts\diskchecker.pl" /ST 01:00

On Windows 2000, you can use the at.exe command to schedule tasks. This command is functionally equivalent to the previous schtasks.exe example:

> at 01:00 /next:Sunday "c:\perl\bin\perl.exe c:\scripts\diskchecker.pl"

The at command is available on Windows Server 2003 as well, but you are better off using schtasks.


Using VBScript
' This code schedules a task to run every Sunday at 1:30AM.     const MON = 1 const TUE = 2 const WED = 4 const THU = 8 const FRI = 16 const SAT = 32 const SUN = 64     ' ------ SCRIPT CONFIGURATION ------ strComputer  = "." strCommand   = "c:\perl\bin\perl.exe c:\scripts\diskchecker.pl" strStartTime = "********013000.000000-240"  ' 01:30 EDT                 'YYYYMMDDHHMMSS.MMMMMM-/+TZO  boolRepeat   = TRUE  ' Repeat the task periodically intWeekDay   = SUN   ' Repeat task every Sunday intMonthDay  = ""    ' Set this if you want the task to repeat monthly boolInteract = FALSE ' Do not interact with the desktop ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "") set objNewTask = objWMI.Get("Win32_ScheduledJob") intRC = objNewTask.Create(strCommand,   _                           strstartTime, _                           boolRepeat,   _                           intWeekDay,   _                           intMonthDay,  _                           boolInteract, _                           intJobID)  if intRC <> 0 then    Wscript.Echo "Error creating task: " & intRC else    WScript.Echo "Successfully scheduled task."    WScript.Echo "JobID: " & intJobID end if

Discussion

Recipes 5.3-5.5 describe how to set up tasks to run when a user logs on or off, or the system starts up or shuts down, but if you want to schedule a job to run at a specific time, you'll need use the Task Scheduler service. The Task Scheduler service is similar to cron on Unix; it allows you to schedule jobs using a variety of time-based criteria. You can run a job only once, once a week, every Monday and Tuesday night, etc.

The Task Scheduler service runs under the LocalSystem account by default. That means that scheduled tasks will have full access to the local system, but if the job needs to access any network resources, it won't have sufficient permissions. In this case, you can do one of two things. You can either run the Task Scheduler service under a different set of credentials (see Recipe 7.4) or you can set a user account and password the task should run as. The Scheduled Tasks applet lets you set the user account and password by entering the account name beside the Run as field and clicking the Set Password button. You can do the same thing using the schtasks.exe utility by specifying the /RP and /RU options. You can get more information on this by running schtasks /create /? from a command line.

Often I find that after I create a new scheduled task, I want to run it once to make sure it does what I expect. You can force a scheduled task to run immediately in the Scheduled Tasks applet by right-clicking it and selecting Run. Likewise, with the schtasks utility you can do the same using the /Run option. Here is an example:

> schtasks /Run /TN "Disk Space Checker"

With the at.exe command, you can create scheduled tasks, but they are a little different and less flexible than the ones you create using schtasks or the Scheduled Tasks applet. You cannot create a task using at that relies on alternate credentials like you can schtasks. Also, scheduled tasks created using schtasks cannot be displayed or modified using at. Another difference is that at tasks are represented by a number (id) that is automatically generated when you create the task; whereas with schtasks, you can assign a name to a given task.

Win32_ScheduledJob represents at jobs, so it is not possible to set alternate credentials when creating a new task with it. And if you query for all Win32_ScheduledJob objects, you are going to get only the jobs created with the at command or Win32_ScheduledJob class, not necessarily all the scheduled tasks.

See Also

Recipe 5.8, Recipe 5.9, and Recipe 7.4



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net