I have made a batch script for backup recently. Somewhere in the middle of script I'll have to wait for some time to be reached and then resume the next line of the script. I've scheduled the script at 4:00PM and after the wait command the next line should start at exactly 5:30PM. I thought of using SLEEP
command but it's not sure that the commands used before the wait command will end up at certain time(due to inconsistent file sizes) but it's sure that they will be done by 5:00 or 5:10 and next it should execute wait command which waits for certain system clock. I'm checking if there is any command that waits or sleeps until the time specified reaches the system time and resumes there after. Anybody came across this situation and how was that resolved?
- 1,653
- 8
- 36
- 66
3 Answers
You're using two tags, WinXP and Server-2008-R2, so I'm not sure which environment you're running in.
Both environments have a command called schtasks
that allows setting up scheduled tasks from the command-line, though the capabilities of this tool varies with the platform. It will allow you to set up a one-time task that will call the remainder of your script. It'll have to be in a separate file, but it'll get executed a single time at 5:30pm.
XP Syntax: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/schtasks.mspx
Server-2008 Syntax: http://technet.microsoft.com/en-us/library/cc725744%28WS.10%29.aspx#BKMK_once
- 131,083
- 18
- 173
- 296
-
will it run at 5:30 if I append `schtaskts` schedule command in place of wait?. – user53864 Jan 29 '11 at 12:47
-
will it wait till 5:30 and start schedule command? – user53864 Jan 29 '11 at 15:29
Probably not the best way to do this, but here goes:
:loop
if %TIME% LSS 17:30:00.00 goto loop
echo Continuing at %TIME%
- 60,515
- 14
- 113
- 148
-
1It may work and I should try that and ofcourse as you said it's not recommended making server busy for unnecessary work. – user53864 Jan 29 '11 at 12:52
-
1It works but it may be continuously executing the loop statement til 17:30. Isn't it possible like `set n=presenttime-17:30` and then `sleep n` something like that which you could do? – user53864 Jan 31 '11 at 11:49
-
@user: Most of that type of command take a number of seconds as a parameter. You can do a calculation to get that value (it would be 17:30-presenttime rather than the way you had it, but the syntax would be more complex). Take a look at the `timeout` command and check the Resource Kits for a `sleep` command. – Dennis Williamson Jan 31 '11 at 14:53
-
I don't know about any command to wait for a specific time but you might find some Googling for it. We solve similar issues by chopping the batch files into separate parts and scheduling them individually.
- 101
- 1
-
Actually I don't want to run separately as I have many scheduled tasks and it's a bit confusing when I want to search for a particular task. So I thought of making it a single batch script. – user53864 Jan 29 '11 at 12:50