Run Sitecore scheduled task at the same time every day

Yesterday I helped a user on stackoverflow with this question:

Is it possible to run a Sitecore scheduled task at the exact time every day?

The quick answer is no. But it’s possble to get it close to the same time every day.

Scheduled tasks are run in sequence by the Sitecore scheduler. The scheduler checks within a certain interval (defined in the web.config in the /scheduling/frequency and /scheduling/agent settings) for tasks to be run. If a task is over due, it is run, and the time where the task finished is recorded as the next checkpoint.

It is not possible to define a certain time a day the task needs to run. It’s only possible to determine which days a task needs to run, and the interval.

So how do you make a task run once a day, at a certain time?

Well, instead of configuring the task to run every 23:59:59, I make my task run every minute, but only executing the functionality once a day. Inside the task I check the time, and only if the time is inside a certain interval I execute the functionality.

Here is how to do it:

1) Create yout task and make it run every 1 minute or 5 minutes (or at least twice as often as your interval).

2) Define an interval where the task is allowed to run. For example at night between 01:00 am and 02:00 am.

3) Build your task in the following manner:

public class TaskRunningOnceAday
  public void Execute(Item[] itemArray, CommandItem commandItem, ScheduleItem scheduleItem)
    if (!IsDue(scheduleItem))


  /// <summary>
  /// Determines whether the specified schedule item is due to run.
  /// </summary>
  /// <remarks>
  /// The scheduled item will only run between defined hours (usually at night) to ensure that the
  /// task is run once a day
  /// Make sure you configure the task to run at least double so often than the time span.
  /// </remarks>
  private bool IsDue(ScheduleItem scheduleItem)
    DateTime time;
    DateTime time2;

    DateTime.TryParse("01:00:00", out time);
    DateTime.TryParse("02:00:00", out time2);

    return (CheckTime(DateTime.Now, time, time2) && !CheckTime(scheduleItem.LastRun, time, time2));

  private bool CheckTime(DateTime time, DateTime after, DateTime before)
    return ((time >= after) && (time <= before));


The scheduleItem.LastRun property contains the date and time from where the task was last run.

The function “IsDue” checks to see if the task has run within the selected interval. If not, it returns true and the task may execute it’s functionality. If it has already run, it returns false, and the functionality is skipped.

The functionality will not give you dead-on accuracy but will do for most jobs.

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c#, Sitecore 6 and tagged , , , , . Bookmark the permalink.

14 Responses to Run Sitecore scheduled task at the same time every day

  1. Jenn says:

    extra ) is a typo?
    DateTime.TryParse(“02:00:00”, out time2))

  2. anonymous says:

    Nice article. I took a slightly different approach. I schedule the task to run every 23 hours (or something less than 24 hours minus the scheduling interval). Then each time the command executes, I re-write the start date of the schedule such that it does not execute until 24 hours after the current start date.

    This may be slightly more efficient as the code for the command does not have to run every each time the scheduler kicks in (as yours does).

    Your solution definately requires less code though.

  3. Pingback: Using Sitecore Jobs « Brian Pedersen’s Sitecore and .NET Blog

  4. Robbert Hock says:

    Just to let people know, what Brian means with: “I make my task run every minute”, just fill this in for the Schedule to work: ||127|00:01:00 in the Schedule field, else if you leave the Schedule field empty, It will receive a flag after the first run and mark the schedule as “IsDue”.

    Robbert Hock
    Sitecore MVP

  5. Pingback: Sitecore scheduling with Windows task scheduler - NewGuid.Net

  6. Josh says:

    This is an older article but I am seeing an issue with the LastRun it gets set as soon as the job run, meaning that by the time I am inside my custom Task code, inside the IsDue method the scheduleItem.LastRun has already been updated. So if the After is say 09:00:00 and the Before is 10:00:00 and we run every minute the LastRan is already updated so the call to:

    return (CheckTime(DateTime.Now, after, before) && !CheckTime(scheduleItem.LastRun, after, before));

    !CheckTime(scheduleItem.LastRun, after, before) is always false.

    Did you experience this? I am using Sitecore 6.6 for this so maybe some Job handling has changed?

  7. briancaos says:

    I haven’t seen this. The article was probably written around Sitecore 6.1 or so. Maybe you are running your task in Async mode (whish by Sitecore 6.6 has become the default setting?). I guess that if you run in async mode, the LastRun is updated as the async job starter is finished, which is a few clock cycles after the job started.

  8. Pingback: Scheduled Task Basics | Sitecore basics!

  9. Pingback: Do you know why you check Async Checkbox while configuring Scheduled Task? | Sitecore basics!

  10. Pingback: Parameterized Sitecore Scheduled Task Agent | Sitecore stuff

  11. Pingback: One more way to Run Sitecore scheduled task at the same time every day | Sitecore basics!

  12. Pingback: Tuning ClayTablet integration with Sitecore – Sitecore Architecture

  13. Pingback: Sitecore Scheduled Tasks – Run on certain server instance | Brian Pedersen's Sitecore and .NET Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s