程式扎記: [Windows 技巧] Windows 的 cron job (定時/周期性執行工作)

標籤

2010年11月14日 星期日

[Windows 技巧] Windows 的 cron job (定時/周期性執行工作)


轉載自 這裡
前言 :
命令 Schtasks : Schedules commands and programs to run periodically or at a specific time. Adds and removes tasks from the schedule, starts and stops tasks on demand, and displays and changes scheduled tasks.

使用說明 :
語法 :
schtasks /create /tn TaskName /tr TaskRun /sc schedule [/mo modifier] [/d day] [/m month[,month...] [/i IdleTime] [/st StartTime] [/sd StartDate] [/ed EndDate] [/scomputer [/u [domain\]user /p password]] [/ru {[Domain\]User | "System"} [/rp Password]] /?

參數 :
/tn TaskName : Specifies a name for the task.
/tr TaskRun :
Specifies the program or command that the task runs. Type the fully qualified path and file name of an executable file, script file, or batch file. If you omit the path, SchTasks.exe assumes that the file is in the Systemroot\System32 directory.

/sc schedule :
Specifies the schedule type. Valid values are MINUTE, HOURLY, DAILY, WEEKLY, MONTHLY, ONCE, ONSTART, ONLOGON, ONIDLE.

/mo modifier :
Specifies how often the task runs within its schedule type. This parameter is required for a MONTHLY schedule. This parameter is valid, but optional, for a MINUTE, HOURLY, DAILY, or WEEKLY schedule. The default value is 1.

/m month[,month...] :
Specifies a month of the year. Valid values are JAN - DEC and * (every month). The /m parameter is valid only with a MONTHLY schedule. It is required when the LASTDAY modifier is used. Otherwise, it is optional and the default value is * (every month).

/i IdleTime :
Specifies how many minutes the computer is idle before the task starts. Type a whole number from 1 to 999. This parameter is valid only with an ONIDLE schedule, and then it is required.

/st StartTime :
Specifies the time of day that the task starts in HH:MM:SS 24-hour format. The default value is the current local time when the command completes. The /st parameter is valid with MINUTE, HOURLY, DAILY, WEEKLY, MONTHLY, and ONCE schedules. It is required with a ONCE schedule.

/sd StartDate :
Specifies the date that the task starts in MM/DD/YYYY format. The default value is the current date. The /sd parameter is valid with all schedules, and is required for a ONCE schedule.

/ed EndDate :
Specifies the last date that the task is scheduled to run. This parameter is optional. It is not valid in a ONCE, ONSTART, ONLOGON, or ONIDLE schedule. By default, schedules have no ending date.

/s Computer :
Specifies the name or IP address of a remote computer (with or without backslashes). The default is the local computer.

/u [domain\]user :
Runs the command with the permissions of the specified user account. By default, the command runs with the permissions of the user logged on to the computer running SchTasks.

/p password :
Specifies the password of the user account specified in the /u parameter. This parameter is required when the /u parameter is used.

/ru {[Domain\]User | "System"} :
Runs the tasks with the permission of the specified user account. By default, the task runs with the permissions of the user logged on to the computer running SchTasks.

/d day :
Specifies a day of the week or a day of a month. Valid only with a WEEKLY or MONTHLY schedule.

/rp Password :
Specifies the password of the user account that is specified in the /ru parameter. If you omit this parameter when specifying a user account, SchTasks.exe prompts you for the password and obscures the text you type. Tasks run with with permissions of the NT Authority\System account do not require a password and SchTasks.exe does not prompt for one.

/?: Displays help at the command prompt.

註解 :
• Using the /s, /u, and /p command-line options
The /u and /p command-line options are available only when you use /s. You must use /p with /u to provide the user's password.
• SchTasks.exe does not verify program file locations or user account passwords. If you do not enter the correct file location or the correct password for the user account, the task is created, but it does not run. Also, if the password for an account changes or expires, and you do not change the password saved in the task, then the task does not run.
• The NT Authority\System account does not have interactive logon rights. Users do not see and cannot interact with programs run with system permissions.
• Each task runs only one program. However, you can create a batch file that starts multiple tasks, and then schedule a task that runs the batch file.
• You can test a task as soon as you create it. Use the run operation to test the task and then check the SchedLgU.txt file (Systemroot\SchedLgU.txt) for errors.

使用範例 :
* To schedule a task to run every 20 minutes
The following command schedules a security script, Sec.vbs, to run every 20 minutes. Because the command does not include a starting date or time, the task starts 20 minutes after the command completes, and runs every 20 minutes thereafter whenever the system is running. Notice that the security script source file is located on a remote computer, but that the task is scheduled and executes on the local computer.
schtasks /create /sc minute /mo 20 /tn "Security Script" /tr \\central\data\scripts\sec.vbs

In response, SchTasks.exe displays a message explaining that the task will run with the permissions of the current user and requests the current user's password. When you enter the password, SchTasks.exe obscures the text you type.
The task will be created under current logged-in user name.
Please enter the password
************

Then, SchTasks.exe displays a message indicating that the task is scheduled:
SUCCESS: The Scheduled Task "Security Script" has successfully been created.

A query shows the task that the command scheduled:
TaskName Next Run Time Status
========================= ======================== ==============
Security Script 10:50:00 AM , 4/4/2001

* To schedule a command that runs every hour at five minutes past the hour
The following command schedules the MyApp program to run hourly beginning at five minutes past midnight. Because the /mo parameter is omitted, the command uses the default value for the hourly schedule, which is every (1) hour. If this command is issued after 12:05 A.M., the program will not run until the next day.
schtasks /create /sc hourly /st 00:05:00 /tn "My App" /tr c:\apps\myapp.exe

* To delete a task from the schedule of a remote computer
The following command deletes the "Start Mail" task from the schedule of a remote computer. It uses the /s parameter to identify the remote computer.
schtasks /delete /tn "Start Mail" /s Svr16

In response, SchTasks.exe displays the following confirmation message. To delete the task, type y. To cancel the command, type n:
WARNING: Are you sure you want to remove the task "Start Mail" (Y/N )? y
SUCCESS: The Scheduled Task "Start Mail" was successfully deleted.

* To delete all tasks scheduled for the local computer
The following command deletes all tasks from the schedule of the local computer, including tasks scheduled by other users. It uses the /tn * parameter to represent all tasks on the computer and the /f parameter to suppress the confirmation message.
schtasks /delete /tn * /f < /f means force>

In response, SchTasks.exe displays the following success messages indicating that the only task scheduled, SecureScript, is deleted.
SUCCESS: The Scheduled Task "SecureScript" was successfully deleted.

* To display the scheduled tasks on the local computer
The following commands display all tasks scheduled for the local computer. These commands produce the same result and can be used interchangeably.
schtasks < or schtasks /query>

In response, SchTasks.exe displays the tasks in the default, simple table format, as shown in the following table:
TaskName Next Run Time Status
========================= ======================== ==============
Microsoft Outlook At logon time
SecureScript 14:42:00 PM , 2/4/2001
This message was edited 7 times. Last update was at 12/11/2010 14:38:10

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!