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
*

Next 5 Runs

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

* Tools

Code & Implementations

nodejs
const cron = require('node-cron');
const { exec } = require('child_process');

console.log('Daily maintenance scheduler initialized.');

// Schedule task to run daily at 4:00 AM
cron.schedule('0 4 * * *', () => {
    console.log(`[${new Date().toISOString()}] Initiating daily database cleanup...`);
    
    exec('npm run db:cleanup', (error, stdout, stderr) => {
        if (error) {
            console.error(`Error during cleanup: ${error.message}`);
            return;
        }
        if (stderr) {
            console.warn(`Warnings during cleanup: ${stderr}`);
        }
        console.log(`Cleanup completed: ${stdout}`);
    });
}, {
    scheduled: true,
    timezone: "Etc/UTC"
});
Setup notes

Install the node-cron library using 'npm install node-cron' and run this script using a process manager like PM2 to ensure high availability.

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

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

Was this helpful?

Last verified: