Monday, February 21, 2022

[SOLVED] Create Cron job to run after x hours

Issue

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.


Solution

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.



Answered By - vlumi
Answer Checked By - Senaida (WPSolving Volunteer)