How to Schedule Daily Jobs at Midnight | CronBase

cron expression Standard
$ 0 0 * * *

Every day at midnight.

The `0 0 * * *` cron expression schedules a task to execute once every single day precisely at midnight. This consistent daily frequency is widely utilized for routine maintenance operations, including database backups, log rotations, system health reports, and data synchronization tasks across enterprise production environments.

Minute
0
Hour
0
Day of Month
*
Month
*
Day of Week
*
How it works

Next 5 Runs

  • in 3h 21m
  • in 1d 3h
  • in 2d 3h
  • in 3d 3h
  • in 4d 3h

* Tools

Code & Implementations

Bash
#!/usr/bin/env bash
# Production Bash script for daily midnight execution with locking
set -euo pipefail

LOCK_FILE="/var/lock/daily_midnight_job.lock"
LOG_FILE="/var/log/daily_midnight_job.log"

# Implement file locking to prevent concurrent runs
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
    echo "$(date -u) - ERROR: Job is already running. Exiting." >&2
    exit 1
fi

echo "$(date -u) - INFO: Starting daily midnight maintenance..." >> "$LOG_FILE"

# Execute business logic
if ! /usr/local/bin/run-daily-cleanup >> "$LOG_FILE" 2>&1; then
    echo "$(date -u) - ERROR: Daily cleanup failed!" >&2
    exit 1
fi

echo "$(date -u) - INFO: Daily maintenance completed successfully." >> "$LOG_FILE"
Setup notes

Save this script to /usr/local/bin/daily-cleanup.sh, make it executable with chmod +x, and add '0 0 * * * /usr/local/bin/daily-cleanup.sh' to your system crontab.

Last verified:

Partner BetterStack

Monitor this schedule in production

Get alerted the moment this cron job fails, is late, or doesn't run. BetterStack tracks execution, duration, and output — no infrastructure required.

Platform Equivalents

AWS EventBridge

Standard cron expressions often need conversion for AWS EventBridge schedules.

EventBridge Rule
cron(0 0 * * ? *)

Systemd Timer

OnCalendar*-*-* 00:00:00

my-task.timer
[Unit]
Description=Timer for cron expression: 0 0 * * *

[Timer]
OnCalendar=*-*-* 00:00:00
Persistent=true

[Install]
WantedBy=timers.target

Last verified:

Frequently Asked Questions

What happens to a midnight cron job during Daylight Saving Time (DST) changes?

If your system runs on a local timezone that observes DST, the job may execute twice (when the clock falls back) or be skipped entirely (when the clock springs forward). To prevent this operational unpredictability, configure your servers and cron daemon to use Coordinated Universal Time (UTC) exclusively.

How can I prevent multiple daily cron jobs from overloading my database at midnight?

Avoid scheduling all daily tasks exactly at midnight. Implement random jitter within your scripts (e.g., sleeping for a random duration between 1 and 600 seconds before starting execution) or stagger the cron schedules across a broader maintenance window (such as 01:00, 02:00, etc.).

How do I monitor if a daily cron job failed to run or silent-failed?

Since daily jobs run infrequently, passive logging is insufficient. Implement an active 'dead-man's snitch' system where the job pings an external monitoring service upon successful completion. Set up alerts to trigger if no ping is received within 15 minutes after midnight.

Can I run a daily midnight job on weekdays only instead of every day?

Yes, you can modify the day-of-week field. To restrict execution to Monday through Friday at midnight, change the cron expression from '0 0 * * *' to '0 0 * * 1-5'. This ensures your weekend resources are not spent on business-day tasks.

What is the best way to handle long-running execution of a daily midnight task?

Ensure your scripts are idempotent and implement file locking using tools like flock in Bash or application-level locks. This prevents a delayed or hung instance from overlapping with the next day's scheduled execution, which could lead to data corruption or resource exhaustion.

* Explore

Related expressions you might need

Last verified:

Was this helpful?