banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

Execute program at specified time period in Windows Task Scheduler.

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.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.