Skip to main content
CheckTown
Generators

Crontab Generator: Build Cron Expressions for Scheduled Tasks

Published 6 min read
In this article

What Is Crontab?

Crontab (cron table) is a time-based job scheduler found in Unix-like operating systems. It allows you to schedule commands or scripts to run automatically at specified intervals — from every minute to once a year. The cron daemon reads crontab files and executes the scheduled commands at their designated times.

Each user on a system can have their own crontab file, and there is also a system-wide crontab. The crontab file contains a list of cron jobs, where each line defines a schedule using a five-field time expression followed by the command to execute.

Cron Expression Syntax

A cron expression consists of five fields separated by spaces, each representing a unit of time. Special characters allow you to define complex schedules:

  • Minute (0-59) — specifies which minute of the hour the job should run
  • Hour (0-23) — specifies which hour of the day in 24-hour format
  • Day of month (1-31) — specifies which day of the month
  • Month (1-12) — specifies which month of the year
  • Day of week (0-7, where both 0 and 7 represent Sunday) — specifies which day of the week

Special characters include: * (any value), / (step values, e.g., */5 means every 5 units), - (ranges, e.g., 1-5 means Monday through Friday), and comma (lists, e.g., 1,15 means the 1st and 15th).

Common Cron Patterns

Here are the most frequently used cron expressions that cover the majority of scheduling needs:

  • */5 * * * * — every 5 minutes, commonly used for health checks and monitoring scripts
  • 0 * * * * — every hour at minute 0, suitable for hourly data syncs or report generation
  • 0 0 * * * — daily at midnight, the standard schedule for backups, log rotation, and cleanup tasks
  • 0 0 * * 1-5 — every weekday at midnight, useful for business-day-only processing
  • 0 0 1 * * — first day of every month at midnight, typically used for monthly reports and billing
  • 0 9,17 * * 1-5 — at 9 AM and 5 PM on weekdays, ideal for start-of-day and end-of-day notifications

Try it free — no signup required

Build a Cron Expression →

Common Use Cases

Cron jobs automate virtually any repetitive system task. Here are the most common applications:

  • Log rotation and cleanup — automatically compress, archive, or delete old log files to prevent disk space issues
  • Database backups — schedule regular mysqldump, pg_dump, or mongodump commands to create consistent backup snapshots
  • Report generation — compile and email daily, weekly, or monthly reports from application data
  • Cache clearing — periodically purge expired cache entries to free memory and ensure data freshness
  • System monitoring — run health checks, disk space alerts, and uptime verification scripts at regular intervals

Tips and Pitfalls

Even experienced developers encounter common cron pitfalls. Keep these tips in mind to avoid scheduling headaches:

  • Timezone awareness — cron uses the system timezone by default. Set TZ in your crontab or use UTC to avoid confusion during daylight saving time changes
  • Overlapping jobs — if a job takes longer than the interval, multiple instances can run simultaneously. Use flock or a PID file to prevent overlap
  • PATH issues — cron runs with a minimal environment. Always use absolute paths for commands and explicitly set PATH at the top of your crontab
  • Output handling — cron emails stdout and stderr to the user by default. Redirect output to a log file or /dev/null to avoid filling up the mail spool
  • Debugging — check /var/log/syslog or /var/log/cron for execution records. Test your command manually before adding it to crontab

Frequently Asked Questions

Can cron schedule jobs with second-level precision?

Standard cron does not support seconds — the smallest unit is one minute. If you need second-level precision, you can use a workaround like running a script every minute that internally loops with sleep commands, or switch to a dedicated scheduler like systemd timers which support sub-minute intervals.

What is the difference between cron and systemd timers?

Cron is the traditional Unix scheduler that uses crontab files with five-field time expressions. Systemd timers are a newer alternative on Linux that offer features like sub-minute precision, calendar event syntax, persistent timers that catch up on missed runs, and better logging integration. Cron is simpler and more portable; systemd timers are more powerful but Linux-specific.

How do I test a cron job before scheduling it?

First, run the exact command manually in your terminal to verify it works. Then simulate the cron environment by running: env -i /bin/sh -c 'your-command' to test with a minimal environment similar to cron. Finally, schedule it to run every minute (* * * * *), verify it executes correctly in the cron log, then change to your desired schedule.

Related Tools