Run Weekly Maintenance at 2 AM Sunday | CronBase

cron expression Standard
$ 0 2 * * 0

Every Sunday morning at two o'clock.

This cron expression schedules a task to run every Sunday at exactly 2:00 AM. It is widely utilized by system administrators for executing heavy weekly maintenance chores, such as database optimization, full system backups, log archiving, and software updates, during a period when user traffic and system load are typically at their lowest.

Minute
0
Hour
2
Day of Month
*
Month
*
Day of Week
0

Next 5 Runs

  • in 1d 16h
  • in 8d 16h
  • in 15d 16h
  • in 22d 16h
  • in 29d 16h

* Tools

Code & Implementations

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

// Schedule task to run every Sunday at 2:00 AM
cron.schedule('0 2 * * 0', () => {
  console.log('[INFO] Starting weekly database maintenance at:', new Date().toISOString());
  
  exec('/usr/local/bin/db-optimize.sh', (error, stdout, stderr) => {
    if (error) {
      console.error(`[ERROR] Maintenance failed: ${error.message}`);
      return;
    }
    if (stderr) {
      console.warn(`[WARN] Maintenance stderr output: ${stderr}`);
    }
    console.log(`[SUCCESS] Maintenance output: ${stdout}`);
  });
}, {
  scheduled: true,
  timezone: "UTC"
});
Setup notes

Install the node-cron package using npm. Run this script in a PM2 or Docker container to keep the Node process running continuously.

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 2 ? * 0 *)

Systemd Timer

OnCalendarSun *-*-* 02:00:00

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

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

[Install]
WantedBy=timers.target

Frequently Asked Questions

How does Daylight Saving Time affect a job scheduled for 2:00 AM on Sunday?

In regions observing Daylight Saving Time, the local clock jumps directly from 1:59 AM to 3:00 AM in the spring, which will cause your job to be skipped. In the autumn, the hour repeats, potentially triggering the job twice. To prevent this, always set your system timezone and cron daemon to UTC.

What is the best way to handle long-running tasks that exceed one week?

For tasks that might run longer than seven days, implement locking mechanisms (such as flock in Bash, redis-lock, or Kubernetes concurrencyPolicy: Forbid). This prevents a new instance of the job from starting while the previous week's run is still actively executing.

Can I use '7' instead of '0' for Sunday in this cron expression?

Yes, in standard and POSIX-compliant cron implementations, both 0 and 7 represent Sunday. However, to ensure maximum portability across different systems, platforms, and third-party libraries, using 0 is the widely accepted industry standard.

How can I test or dry-run this Sunday morning schedule immediately?

You can temporarily adjust the cron schedule to run every few minutes for testing, or execute the underlying script/command directly in your terminal. For testing the scheduler logic itself without waiting, use tools like `cron-parser` in Node.js or Python's mock library to simulate time-shifts.

How do I prevent CPU spikes if multiple weekly jobs are scheduled at 2:00 AM?

To avoid resource contention and CPU spikes, stagger your jobs by offsetting the minutes field (e.g., 0 2, 15 2, 30 2) or introduce a randomized sleep delay at the beginning of your script execution (e.g., `sleep $((RANDOM % 300))`).

* Explore

Related expressions you might need

Was this helpful?

Last verified: