Execute Daily 4 AM Maintenance Tasks | CronBase

cron expression Standard
$ 0 4 * * *

Every day in the early morning at exactly four o'clock.

The `0 4 * * *` cron expression schedules a job to run once every day at exactly 4:00 AM. This daily cadence is highly popular in production environments for executing resource-intensive operations, such as database optimization, system backups, and log rotation, during off-peak hours when user activity is minimal.

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

Next 5 Runs

  • in 7h 22m
  • in 1d 7h
  • in 2d 7h
  • in 3d 7h
  • in 4d 7h

* Tools

Code & Implementations

Bash
#!/usr/bin/env bash

# Ensure the script exits immediately if any command fails
set -euo pipefail

LOG_FILE="/var/log/daily_backup.log"

# Function to log messages with timestamps
log_message() {
    echo "[$(date -u +'%Y-%m-%dT%H:%M:%SZ')] $1" >> "$LOG_FILE"
}

log_message "Starting daily maintenance task..."

# Implement jitter to avoid thundering herd issues
SLEEP_TIME=$((RANDOM % 300))
log_message "Sleeping for ${SLEEP_TIME} seconds to distribute load..."
sleep "$SLEEP_TIME"

# Execute the backup process
if pg_dump -U db_user -h localhost production_db > /backups/prod_$(date +%F).sql; then
    log_message "Backup completed successfully."
else
    log_message "ERROR: Backup process failed!"
    exit 1
fi
Setup notes

Add this script to your crontab by running 'crontab -e' and appending the line: 0 4 * * * /path/to/script.sh >> /var/log/cron_output.log 2>&1

Last verified:

Partner UptimeRobot

Keep this cron job monitored 24/7

UptimeRobot alerts you the moment a scheduled job stops responding. Free plan monitors up to 50 endpoints — no credit card required.

Platform Equivalents

AWS EventBridge

Standard cron expressions often need conversion for AWS EventBridge schedules.

EventBridge Rule
cron(0 4 * * ? *)

Systemd Timer

OnCalendar*-*-* 04:00:00

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

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

[Install]
WantedBy=timers.target

Last verified:

Frequently Asked Questions

How does Daylight Saving Time affect a 4:00 AM cron job?

Depending on your local timezone, a 4:00 AM job is usually safe from the 2:00 AM DST shift. However, if your system transitions timezone offsets, the local time of execution will shift relative to UTC. Running servers on UTC avoids this entirely.

What happens if a previous day's job is still running at 4:00 AM?

Standard cron daemons will launch a new instance of the job regardless of whether the previous one finished. To prevent overlapping executions, use a locking wrapper like 'flock' in Bash or application-level distributed locks.

How can I add jitter to avoid a thundering herd at 4:00 AM?

You can introduce a random sleep at the start of your execution script. For example, in Bash, running 'sleep $((RANDOM % 300))' will delay the actual task by up to 5 minutes, distributing the resource load across your cluster.

Can I run this job only on weekdays at 4:00 AM?

Yes, you can modify the expression to '0 4 * * 1-5'. This restricts the daily execution to Monday through Friday, which is ideal for business-centric processes that do not need to run over the weekend.

How do I capture and debug failures for a daily 4:00 AM task?

You should redirect both standard output and standard error to a dedicated log file, or forward them to a centralized logging system. Additionally, integrate an alerting tool (like PagerDuty or Slack webhooks) to notify your on-call team immediately if the job exits with a non-zero status.

* Explore

Related expressions you might need

Last verified:

Was this helpful?