A simple requirement is to set up a task scheduler on a Windows server to execute a specific program within a specified time period from 9:00 to 15:00. However, unlike crontab, Windows task scheduler does not support setting a specific time range and can only be set to start once every hour.
Method 1: Launch the Bootstrap program and determine whether to continue executing the task based on the current time.
Get the current time as a string.
SET curr_time=%TIME:~0,-5%
SET curr_time_str=%curr_time::=%
After obtaining the time, use logical conditions to determine whether to go to execute the corresponding code.
Complete code:
@echo off
ECHO "Time Schedule Bootstrap"
SET curr_time=%TIME:~0,-5%
SET curr_time_str=%curr_time::=%
IF %curr_time_str% leq 0900 (GOTO time_cancel) ELSE (
IF %curr_time_str% leq 1500 (GOTO time_exec) ELSE (
GOTO time_cancel
)
)
exit 0
ECHO "Call CMD.exe"
CMD.exe
exit 0
ECHO "Canceled"
exit 10
Method 2: Set to start once a day at a specified hour, multiple settings are required, skipping this part here.