Every 5 min cron job between specific time (Windows server 2008 and batch file)

1

Previous colleague set up cron job every 5 min for 24 hrs. Now I need to modify this only running between 7am to 7pm.

I had never learned cron job so I googled and tried but it didn't work.

Task Scheduler is set as below

  • Begin the task
    On a schedule
  • Settings
    One time
  • Advanced settings
    Repeat task every 5 minutes for a duration of Indefinitely
    Stop task if it runs longer than 30 minutes
    Enabled

And I modified the batch file from

C:\PHP\php.exe -f C:\path\cron.php five-mins

to

*/5 7-19 * * * C:\PHP\php.exe -f C:\path\cron.php five-mins

Even I added */5 7-19 * * * to batch file, it doesn't work.

It would be appreciative if someone can help me. Thanks in advance and thanks for taking your time.

hasmai

Posted 2015-06-11T08:39:29.310

Reputation: 21

ockquote>

Big Chris, thanks for the suggestion I had a look and this could be one of the solution however this time I will use RedGrittyBrick's batch file coding. Anyway thank you very much!

– hasmai – 2015-06-11T10:51:27.567

Answers

2

Windows native job scheduler is not configured the same way as Unix/Linux cron,

You cannot change the scheduling by editing the contents of a batch file.

You have limited options in the Task scheduler

enter image description here

You could alter that batch file to exit early if the time of day is outside a specific range.

An answer in stackoverflow suggests

set "currentTime=%Time: =0%"
set flag=false
if %currentTime% geq 07:00 if %currentTime% leq 19:00 set flag=true
if %flag%==true (
   # your existing commands
   # go here
)

RedGrittyBrick

Posted 2015-06-11T08:39:29.310

Reputation: 70 632

ockquote>

RedGrittyBrick, thank you very much for your explanation and details. Yes it works great! I never thought I could use coding for batch file as I hadn't self-learned it yet. Again, I appreciated it Thank you!

– hasmai – 2015-06-11T10:53:42.913