1

I have a job that runs every 15 minutes and gets a datetime. The batch file contains the following PowerShell command to get the current datetime minus 15 minutes:

for /f "delims=" %%a in ('"powershell [DateTime]::Now.AddMinutes(-15).ToString('yyyy-MM-ddTHH:mm:ss')"') do set "dt=%%a"

I write the results of the PowerShell command in my logs. I've been able to notice that every 5 or so job runs, there is a small delay of anywhere between 1-7 seconds. For example, if the job is scheduled to run at 15:00:00, and there is a delay of 5 seconds, my logs will show 14:45:05 as the datetime returned by the PowerShell command.

What could be the reason for this delay? Perhaps the PowerShell command takes longer than expected sometimes? Or could it be that the OS is not running the task on time because it might be struggling to handle resources?

Please let me know if I should provide any more info - I'm having a hard time to be less general/open-ended with the question.

jgozal
  • 1,480
  • 6
  • 22
  • 43
  • 2
    Why don't you write the script in powershell instead of batch? (also cmd has built-in variables for date/time) – Maximilian Burszley May 14 '18 at 20:00
  • @TheIncorrigible1, just a matter of habit using batch files I guess. I should definitely do that but do you think it might resolve this delay? If so why? – jgozal May 14 '18 at 20:02
  • 1
    @TheIncorrigible1 I get your point in why build hybrid scripts, but how is it related to the delay issue? – Gerhard May 14 '18 at 20:02
  • Probably [because other jobs may be delayed](https://support.microsoft.com/en-us/help/982341/scheduled-tasks-are-delayed-in-windows-server-2008-or-in-windows-vista)? – Maximilian Burszley May 14 '18 at 20:04
  • 1
    Calling an external process and parsing its output isn't always going to be consistent is another factor. – Maximilian Burszley May 14 '18 at 20:05
  • I suggest you run the script manually and see if there are delays. keep in mind that your timing is not perfect on scheduled tasks – Gerhard May 14 '18 at 20:05
  • Thanks for the comments so far all. @TheIncorrigible1 I did find that article on my first google searches but unless I'm interpreting it incorrectly, it says that the tasks need to have a randomly delayed option checked correct? I do not have such a thing in my case. I also wondered if calling PowerShell and parsing the string might be the issue but 7 seconds seems like way too much. – jgozal May 14 '18 at 20:13
  • The hybrid script should not really cause this issue. I run the code on my old i7 gen 4 laptop that is being killed by running perl scripts at the moment and it runs under a second each time. – Gerhard May 14 '18 at 20:21
  • 4
    the OS is not real-time, nor is the task guaranteed to start and run at exactly the start time for the task. run you task under Jenkins and see similar patterns. if you really want more control over this, write a service that runs your tasks, and even then you will see variances. – Kory Gill May 14 '18 at 20:26
  • Thanks @KoryGill – jgozal May 18 '18 at 01:24

0 Answers0