buildcron

Cron Expression to Run at Midnight

The cron expression for midnight is 0 0 * * *: minute 0 of hour 0, the first minute of every day. The shorthand @midnight means the same thing.

CRON → ENGLISH
0 minute0 hour* day of month* month* day of week
VALID EXPRESSION

At 12:00am every day.

0 0 * * *

Standard 5-field crontab syntax. Day of month and day of week are OR'd when both are set.

NEXT 5 RUN TIMES · local time

What it does

Midnight in cron is always 00:00 — the start of a day, never the end. The hour field runs from 0 to 23, so there's no 24 and no way to write “end of day” other than choosing 00:00 of the next day or a time like 23:59. Both @midnight and @daily are aliases for 0 0 * * *.

Midnight is the natural boundary for jobs that work on whole days: closing the day's books, rolling daily log files, resetting daily counters and rate limits, snapshotting a table for yesterday's analytics partition, or expiring content that was only valid “today”. Running right at the boundary means the previous day is complete and nothing from the new day has arrived yet.

A job that runs at 00:00 and processes “today” will usually find an empty day — it needs to process yesterday instead, for example with GNU date -d yesterday +%F (escape the % as \% inside a crontab line). And because other daily jobs on the server may also start at midnight, give heavy work a few minutes' head room — 5 0 * * * — or move it later in the night.

Midnight is also when months and years roll over, so a job at 0 0 * * * crosses those boundaries too. If it names files or partitions by date, derive the name from the date the data belongs to, not from the moment the job happens to start.

How 0 0 * * * works, field by field

FIELDVALUEMEANING
Minute0minute 0
Hour0hour 0 (midnight)
Day of month*every day of the month
Month*every month
Day of week*every day of the week

Put together, cron reads 0 0 * * * as: “At 12:00am every day.

To see further ahead, paste the expression into the cron calculator, which lists the next ten run times and counts how many times the schedule runs in a year.

Frequently asked questions

Can I use 24 for midnight in a cron expression?

No. Valid hours are 0 to 23, so midnight is hour 0. An expression with 24 in the hour field is rejected as out of range.

What's the difference between @midnight and @daily?

There isn't one — both are shorthand for 0 0 * * *. @midnight exists because it reads more clearly when the exact time matters.

How do I run a cron job just before midnight?

Use 59 23 * * * to run at 23:59. That's useful for end-of-day snapshots that must be labelled with the current date.