DevRivet

Blog ·

Cron Expression for First Day of the Month: 0 0 1 * *

Use 0 0 1 * * to run a cron job at midnight on the 1st of every month. Covers the day-of-week OR gotcha and workarounds for last day and first weekday.

Cron Expression Generator

Build, understand and validate cron expressions.

Try it free →

To run a job at midnight on the first day of every month, use the cron expression 0 0 1 * *. It means minute 0, hour 0, day-of-month 1, any month, any day of the week. You can build and preview this schedule in the Cron Expression Generator to check the next run times before you deploy it.

The expression itself is simple. The surprises come from how standard cron combines the two day fields, and from the fact that cron has no built-in way to say "last day of the month." This post covers both.

The first-of-month expression

0 0 1 * *
│ │ │ │ └─ day of week (any)
│ │ │ └─── month (any)
│ │ └───── day of month (1)
│ └─────── hour (0)
└───────── minute (0)

Here are a few variants people often need alongside it:

  • 0 0 1 * *: midnight on the 1st of every month.
  • 0 6 1 * *: 06:00 on the 1st of every month.
  • 30 2 1 * *: 02:30 on the 1st of every month.
  • 0 0 1 1,4,7,10 *: midnight on the 1st of January, April, July and October (quarterly).
  • 0 0 1 1 *: midnight on January 1st only (yearly).

The hour and minute are evaluated in the timezone the cron daemon or scheduler uses. On many servers that is UTC, so check the timezone setting of your environment before you assume local midnight.

Day-of-month and day-of-week are OR-ed

In standard Vixie-style cron, if both the day-of-month and day-of-week fields are restricted (neither is *), the job runs when either field matches. It does not require both to match. This is the most common source of confusion with this schedule.

For example, this expression looks like it should run only on Mondays that fall on the 1st:

0 0 1 * 1

In standard cron it runs on the 1st of every month and on every Monday. Mondays occur about four to five times a month, so that adds about four to five extra Monday runs per month, plus the 1st itself when it is not a Monday.

The rules are:

  • If day-of-month is * and day-of-week is restricted, the job runs only on matching weekdays.
  • If day-of-month is restricted and day-of-week is *, the job runs only on matching dates.
  • If both are restricted, the job runs on days that match either one.
  • If both are *, the job runs every day.

If you want the 1st of the month only, leave day-of-week as *. If you want a weekday-based schedule, leave day-of-month as *. Do not combine them unless you really want the OR behavior. For more on weekday fields, see Cron Every Monday: Expressions and Weekly Schedules.

Why there is no last-day syntax

Standard 5-field cron has no L modifier, so you cannot write "last day of the month" as a single expression. Some schedulers add extensions for this, but those are not part of the standard syntax, and their behavior varies. If your scheduler does not document it, do not rely on it.

The portable workaround is to run the job more often and check the date inside your script. The last day of a month is always 28, 29, 30 or 31, so you can run the job daily and exit early unless tomorrow is the 1st.

A daily schedule at midnight looks like this:

0 0 * * *

Then the script decides whether to do the work. Here is a check in JavaScript (Node.js) that tests whether today is the last day of the month:

function isLastDayOfMonth(date = new Date()) {
  const tomorrow = new Date(date);
  tomorrow.setDate(date.getDate() + 1);
  return tomorrow.getDate() === 1;
}

if (!isLastDayOfMonth()) {
  process.exit(0);
}
// run the monthly job here

The same idea in Python:

import datetime
import sys

tomorrow = datetime.date.today() + datetime.timedelta(days=1)
if tomorrow.day != 1:
    sys.exit(0)
# run the last-day job here

Both checks compare tomorrow's day with 1. That avoids needing to know how many days the month has.

Pitfalls with the last-day workaround

  • Time zones: getDate() and date.today() use the timezone of the machine running the script. If the cron daemon runs in UTC and your business day is in another zone, the check can fire on the wrong day. Compare against the timezone you actually care about.
  • Daylight saving changes: a job scheduled near local midnight can behave unexpectedly on DST transition days. Choosing a time away from midnight, such as 12:00, reduces that risk.
  • Overlapping runs: the daily job runs about 365 times a year, so make sure the early exit is cheap and does not hold locks.

Running on the first business day

Sometimes the requirement is "first weekday of the month," not the literal first. Standard cron cannot express that either, but a small script can handle it. Schedule the job for days 1 through 3, since the first weekday is always within that range. A weekend 1st moves the first weekday to the 2nd or 3rd.

0 0 1-3 * *

The script then needs to know two things: whether today is a weekday, and whether this month's run has already happened. The second part is state, and cron does not store it for you. A marker file keyed by year and month works well. The script checks for the file, exits if it exists, does the work, and then creates the file.

#!/bin/bash
MARKER='/var/lib/myjob/done-'$(date +%Y-%m)
[ -e "$MARKER" ] && exit 0
DOW=$(date +%u)
[ "$DOW" -gt 5 ] && exit 0
# run the first-business-day job here
touch "$MARKER"

Here is how the logic plays out. If the 1st is a Saturday, the runs on the 1st and 2nd exit because it is the weekend, and the run on the 3rd, a Monday, does the work and writes the marker. Later runs that month see the marker and exit.

A few details matter here. date +%u returns 1 for Monday through 7 for Sunday. The marker directory must be on persistent storage, or the check will repeat after a reboot or container restart.

Retries are limited by the schedule. 0 0 1-3 * * runs only on the 1st, 2nd and 3rd, so if the job fails before touch runs, it is retried on the next day only if that day is still 1 to 3. A failure on the 3rd is not retried until the 1st of next month. If you need a retry on later days, widen the day range, or add a separate retry job. If a failure should not trigger another attempt, write the marker first and handle errors separately.

Checking your expression

Before you deploy, confirm that the expression produces the dates you expect. The Cron Expression Generator shows next run times for a 5-field expression, which makes the OR behavior easy to spot. If you run jobs on a fixed interval instead, see Cron Every Day at Midnight for daily variants.

FAQ

How do I run a job on the 15th and also on every Friday?

Use 0 0 15 * 5. Because day-of-month and day-of-week are both restricted, standard cron runs the job on the 15th of every month and on every Friday. That OR behavior is exactly what you want here. If you only wanted the 15th when it falls on a Friday, no single standard expression can do that, so you would need a check in the script.

Does 0 0 1 * * run at midnight in my local timezone?

It runs at midnight in whatever timezone the cron daemon or scheduler uses. Many servers default to UTC. Check your scheduler's settings or the server's timezone before relying on local time.

Can I use L for last day of month in cron?

Not in standard 5-field cron. Some schedulers support an L extension, but it is not portable. Use a daily schedule and a date check in your script instead.

Why did my monthly job run more often than once a month?

Most likely the day-of-week field is not *. When both day-of-month and day-of-week are restricted, standard cron runs on days matching either one. Set day-of-week to * to run only on the 1st.

Cron Expression Generator

Build, understand and validate cron expressions.

Try it free →