Create Cron job to run after x hours

Viewed 1750

I'm using hangfire for scheduling jobs and creating recurring job as:

RecurringJob.AddOrUpdate(() => BackGroundJobManager.FirstJob(), Cron.Daily(4));

This runs the job daily at 4 AM. How can I configure a cron job to run after every 4 hours.

2 Answers

The cron expression to schedule something every four hours is e.g.:

0 */4 * * *

You could build that expression with Cron.HourInterval(4), but it seems to be deprecated. Since those methods just return the cron expression as a string, you could just build your own and use that instead.

Whilst the HourInterval function is being deprecated, you could just create your own as all it does is return the cron schedule.

public static string HourInterval(int interval)
{
    return string.Format("0 */{0} * * *", (object) interval);
}
Related