Search code examples
croncronexpressionhangfire

Hangfire CronExp to run 1 am on a specified day every month


I want to set up a cronExp to run 1 am on a particular day of the month which is added from a query

cronExp = "0 0 1 "[email protected]+ "* *";

I tried the above but I get this not syntactically correct

Please help

Update

I managed to make below work auto.Day =24

cronExp = "0 1 1 */"[email protected]+" *";

but it reads Cron At 01:01 AM, every 24 days is this the same as 01:01 Am on the 24th of everymonth?


Solution

  • Cron expressions can be tricky if you're not used to writing them. Check out https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm for an example of how to write them.

    Yours would best be written as:

    cronExp = "0 1 " + @auto.Day + " * * ";
    

    Your first one didn't work because it was missing a space in the expression between the Day of Month and Month fields (it looked like 0 0 1 24* *). Your second one was setting the minute, hence the 1:01 AM.