Skip to content

Incorrect Behavior in Setting Up a Cron Job for First Monday of the Month #890

Open
@mnkasikci

Description

Description

The cron expression ("5 12 */100,1-7 * 1") for running a cron job on every first Monday of the month at 12:05 PM appears to be having unintended behavior. The linkage between the month and weekday in the given expression is "OR" , but it should be "AND".
(It works every monday + first 7 days of the month, but it should work only if it's monday AND if it's first seven days of the month)
Sources: a blogpost
Crontab.guru

Expected Behavior

The cron job should execute only on the first Monday of the month at 12:05 PM.

Actual Behavior

Instead of running exclusively on the first Monday of the month, the cron job is executing on every Monday and the first seven days of any month.

Possible Fix

No response

Steps to Reproduce

  1. Set the cron expression in your node-cron configuration to "5 12 */100,1-7 * 1".
  2. Create a cron job that runs the function which logs a message.
  3. Run the cron job.
  4. Check the output of the next 10 execution dates:
const cronExpression = "5 12 */100,1-7 * 1";
const job = new CronJob(
  cronExpression,
  function () {
    console.log("You probably won't wait this long, but if you do, you should see that it runs every monday + first seven days of the month.");
    console.log(`Run on : ${new Date().toISOString()}`);
      }, // onTick
  null, // onComplete
  true,
);
for (const nextDate of job.nextDates(10))
  console.log(nextDate.toString());

Context

I was trying to send a certain report to people every first monday of the month. As a temporary solution, i set the cron expression to run every monday , and the onTick() function checks if it's in the first 7 days of the month, and returns without doing nothing otherwise.

Your Environment

I don't think these are relevant, but still providing them

  • cron version3.1.7:
  • NodeJS version: v20.11.1
  • Operating System and version: macOS Sonoma 14.5
  • TypeScript version (if applicable): 5.2.2
  • Link to your project (if applicable): N/A

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    type:bugBug reports and bug fixes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions