Link to home
Start Free TrialLog in
Avatar of jamserra
jamserra

asked on

run batch file every 30 seconds

In the Task Scheduler, it does not allow you to specify repeating a task in seconds.  I want to run a batch file every 30 seconds.  How else can I do this since I can't using Task Scheduler?
ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jamserra
jamserra

ASKER

If I were to place those commands in a batch file, and initially run that batch file from the Task Scheduler, would it continue to run every 30 seconds?  If so, how would I stop the batch file?
Yes, it would continually run.  The Task scheduler would probably be able to end it itself (remember, it has an option to stop the task if it runs x amount of time.

In addition, as I said, if it's the kind of batch file that can test for something to be true, you could force it to end with an IF line.

What does the batch file do?
The batch file calls cscript to execute a script that checks a directory to see if a new version of a file exists and if so, runs a few other commands.  A file is ftp'd to that directory every few minutes, so I want to run the batch file to grab the ftp'd file soon after it appears.
I guess another solution is I could have the Task Scheduler call the batch file every minute, and in the batch file I execute the script, sleep for 30 seconds, and execute the script again.  Does that make sense?
Sure, that could work.

Of you can try to configure the batch file to look for that file too and if it finds it, exit.
Here is a simple VBScript solution - just create the file stop.txt when you want to stop the script.

Dim fs
set fs = CreateObject ( "Scripting.FileSystemObject" )

' Change path of stop file as required.

while not fs.FileExists ("c:\stop.txt")
 Wscript.Sleep 3000
 call DoSomething
Wend

Sub DoSomething
  Wscript.Echo "put your code to execute every 30secs here"
end sub
Obviously change the

Wscript.Sleep 3000 to Wscript.Sleep 30000

If you want 30secs rather than the 3 secs I used for testing ... ;|
for that matter, if you want to do the exit in a batch file, it would be:


:start
REM put your commands here
REM If you want to test for some condition you can exit the loop with
IF Exist c:\stop.txt Goto EOF
SLEEP 30
Goto Start
:EOF

VBScript can be great... but in my opinion, 9 times out of 10, it's excessive for what you want to do.
The problem with those last two solutions is that I am terminal servering into my server and then I logout, so those solutions would mean once I logout they would stop running.
Not if you schedule it with the Scheduled Tasks - create the batch file then have the scheduled tasks run it.
Thanks, this has been very helpful.  One clarification, in your first comment when you say "if the app takes any measurable time, then you'll end up running it 30 seconds after it starts.", don't you mean that the app would run 30 seconds after the commands ended (instead of after it starts)?  So if I first run the batch file at 1:00:00, and it runs a script that takes ten seconds (until 1:00:10), it would then sleep for 30 seconds and start the script again at 1:00:40 (instead of 1:00:30)?
Yes, my apologies for miswording that, that's exactly what I mean.