Skip to content
Home/Guides/Cron Expression Builder — How to Write and Test Cron Schedules Online
Guide·

Cron Expression Builder — How to Write and Test Cron Schedules

Cron expressions are one of those things developers use constantly but rarely memorize. You need to schedule a backup at 2 AM every Sunday, run a cleanup job every 15 minutes, or trigger a report on the first day of each month. The five-field syntax is simple in theory but surprisingly easy to get wrong — especially when combining day-of-month and day-of-week fields, or working with the subtle differences between Linux cron, AWS CloudWatch, and Kubernetes CronJobs.

This guide covers cron syntax from the ground up, common scheduling patterns, platform differences, and the mistakes that cause jobs to run at the wrong time.

What Is a Cron Expression?

A cron expression is a string of five space-separated fields that defines when a scheduled task should run: minute hour day-of-month month day-of-week. Each field accepts specific values, ranges, steps, and wildcards. The expression 0 9 * * 1 means "at minute 0, hour 9, any day-of-month, any month, on Monday" — which translates to every Monday at 9:00 AM.

You would use cron expressions for scheduling database backups, log rotation, data pipeline jobs, email reports, cache clearing, health checks, CI/CD deployments, and any recurring automated task. Cron is the standard scheduling format across Linux crontab, AWS EventBridge, Kubernetes CronJobs, GitHub Actions, and most CI/CD platforms.

How to Build Cron Expressions with FlipMyCase

  1. Open the FlipMyCase Cron Expression Builder.
  2. Use the visual editor to set minute, hour, day, month, and weekday.
  3. See the expression translated to plain English and the next scheduled run times.
  4. Copy the expression for use in your crontab, AWS rule, or Kubernetes manifest.

The builder validates your expression in real time and shows warnings for common mistakes like overly frequent schedules or conflicting day fields.

Code Examples for Cron Scheduling

JavaScript (Node.js with node-cron)

const cron = require('node-cron');

// Run every 15 minutes
cron.schedule('*/15 * * * *', () => {
  console.log('Running cleanup task');
});

// Run at 2:30 AM every day
cron.schedule('30 2 * * *', () => {
  console.log('Running daily backup');
});

// Run at 9 AM on weekdays (Monday-Friday)
cron.schedule('0 9 * * 1-5', () => {
  console.log('Sending daily report');
});

// Run on the 1st of every month at midnight
cron.schedule('0 0 1 * *', () => {
  console.log('Generating monthly report');
});

// Validate a cron expression
console.log(cron.validate('*/5 * * * *'));   // true
console.log(cron.validate('60 * * * *'));    // false (minute max is 59)

Python (with schedule and APScheduler)

# Using APScheduler with cron triggers
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger

scheduler = BlockingScheduler()

# Every 15 minutes
@scheduler.scheduled_job(CronTrigger.from_crontab('*/15 * * * *'))
def cleanup():
    print('Running cleanup')

# Daily at 2:30 AM
@scheduler.scheduled_job(CronTrigger.from_crontab('30 2 * * *'))
def backup():
    print('Running backup')

# Weekdays at 9 AM
@scheduler.scheduled_job(CronTrigger.from_crontab('0 9 * * 1-5'))
def daily_report():
    print('Sending report')

# Parse and display next run times
from croniter import croniter
from datetime import datetime

cron = croniter('0 9 * * 1-5', datetime.now())
for _ in range(5):
    print(f'Next run: {cron.get_next(datetime)}')

# scheduler.start()  # Uncomment to run

Bash (Linux crontab)

# Edit crontab
crontab -e

# Common cron patterns:
# ┌─── minute (0-59)
# │ ┌─── hour (0-23)
# │ │ ┌─── day of month (1-31)
# │ │ │ ┌─── month (1-12)
# │ │ │ │ ┌─── day of week (0-6, Sun=0)
# │ │ │ │ │

# Every 5 minutes
*/5 * * * * /usr/bin/python3 /home/user/cleanup.py

# Daily at 2:30 AM
30 2 * * * /usr/bin/pg_dump mydb > /backups/daily.sql

# Every Monday at 9 AM
0 9 * * 1 /usr/bin/python3 /home/user/weekly_report.py

# First day of every month at midnight
0 0 1 * * /usr/bin/python3 /home/user/monthly_billing.py

# Every weekday at 6 PM
0 18 * * 1-5 /usr/bin/python3 /home/user/eod_summary.py

# List current crontab entries
crontab -l

# Redirect output to log file
*/15 * * * * /home/user/task.sh >> /var/log/task.log 2>&1

Real-World Use Cases

Database backups. Schedule pg_dump or mysqldump to run daily at 2 AM when traffic is lowest. Use the Cron Expression Builder to verify the schedule, then add the expression to your crontab or Kubernetes CronJob manifest.

Log rotation and cleanup. Old log files consume disk space. Schedule a cleanup job every 6 hours (0 */6 * * *) to compress logs older than 24 hours and delete logs older than 30 days. This prevents disk-full incidents without manual intervention.

Scheduled reports and notifications. Business teams need daily, weekly, and monthly reports. Schedule data aggregation at off-peak hours and email delivery in the morning: 0 8 * * 1-5 for a weekday 8 AM report.

Cache warming and invalidation. After a nightly data import, schedule a cache warming job to pre-compute expensive queries. 30 3 * * * runs at 3:30 AM, after the 2 AM data import completes.

Common Mistakes and Gotchas

The most common mistake is confusing day-of-month and day-of-week behavior. In standard cron, if both fields are set (not *), the job runs when either condition is true (OR logic), not both (AND logic). 0 9 15 * 1 runs on the 15th of every month AND every Monday, not "Monday the 15th only."

Forgetting that cron uses the server's timezone causes jobs to run at unexpected times. A server in UTC running 0 9 * * * fires at 9 AM UTC, which is 4 AM Eastern. Always verify your server's timezone with timedatectl and document which timezone your cron expressions assume. Convert times with the Unix Timestamp Converter.

Environment variables are not loaded in cron. Your crontab job runs with a minimal environment — $PATH, $HOME, and other variables you rely on in interactive shells may not exist. Always use absolute paths for commands (/usr/bin/python3 not python3) and source your profile if needed.

Output goes nowhere by default. If a cron job fails, you will not see the error unless you redirect output. Always append >> /var/log/job.log 2>&1 to capture both stdout and stderr.

Frequently Asked Questions

What is a cron expression? A cron expression is a string of five fields separated by spaces that defines a schedule for recurring tasks. The fields represent minute, hour, day of month, month, and day of week, in that order. Cron expressions are used in Unix/Linux systems, cloud platforms like AWS, and CI/CD tools to automate task execution at specific intervals.

How do I run a cron job every 5 minutes? To run a cron job every 5 minutes, use the expression */5 * * * *. The */5 in the minute field means "every 5th minute." The asterisks in the remaining fields mean "every hour, every day of the month, every month, and every day of the week," so the job runs continuously at five-minute intervals.

What is the difference between * and ? in cron expressions? The asterisk (*) means "every possible value" for a field. The question mark (?) means "no specific value" and is only used in the day-of-month and day-of-week fields in extended cron formats like Quartz. It resolves ambiguity when you specify one day field but want the other to be unrestricted.

Conclusion

Cron expressions are the universal standard for scheduling recurring tasks across Linux, AWS, Kubernetes, and CI/CD systems. The five-field syntax is powerful but easy to get wrong, especially with day-of-week/month interactions and timezone differences.

The FlipMyCase Cron Expression Builder lets you visually construct and validate expressions with plain-English translation and next-run previews. For programmatic scheduling, the JavaScript, Python, and Bash examples above cover the most common patterns. Use the Unix Timestamp Converter for timezone debugging and the Regex Tester for validating cron expression formats in your code.

Try Cron Builder

Free, no signup, runs in your browser. Try it now.

Frequently Asked Questions

What is a cron expression?

A cron expression is a string of five fields (minute, hour, day-of-month, month, day-of-week) that defines a recurring schedule. For example, '0 9 * * 1' means every Monday at 9:00 AM. Cron is used in Linux, AWS, Kubernetes, and CI/CD systems.

What does the asterisk mean in cron?

An asterisk (*) means 'every value' for that field. In '* * * * *', every field is wildcarded, so the job runs every minute. In '0 * * * *', the minute is 0 and the rest are wildcards, so it runs at the top of every hour.

How do I run a cron job every 5 minutes?

Use '*/5 * * * *'. The */5 in the minute field means every 5th minute. Similarly, */15 means every 15 minutes, */30 means every 30 minutes, and 0 */2 * * * means every 2 hours at minute 0.

What is the difference between cron and crontab?

Cron is the scheduling daemon that runs jobs. Crontab (cron table) is the file where you define your schedule entries. You edit it with 'crontab -e' on Linux. Each line is a cron expression followed by the command to run.

Free Text Tools