Skip to main content

Cron Job

Pronunciation
KRAHN job
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/cron-job

In short

A cron job is a command or script that runs automatically on a repeating schedule, such as every night at 2 a.m., defined by a five-field cron expression.

What is a cron job?

A cron job is a task that runs on a schedule instead of being started by a person or a request. The name comes from cron, a background service on Unix-like systems such as Linux and macOS that has existed since the 1970s and checks every minute whether any scheduled job is due. Each user's jobs are listed in a file called a crontab, short for cron table, which you edit with crontab -e.

Each line in a crontab has a schedule followed by the command to run. The schedule is a cron expression with five fields: minute, hour, day of the month, month, and day of the week, where * means every value. For example, 0 2 * * * means 2:00 every day and */15 * * * * means every 15 minutes, and the same syntax is used far beyond classic cron, in Kubernetes CronJobs, CI/CD pipelines, and cloud and serverless schedulers.

A cron job is like an alarm clock that also does the chore for you when it rings. Typical uses include nightly database backups, daily report emails, cleaning up expired sessions and temporary files, renewing TLS certificates, and syncing data from other systems.

Cron jobs are often confused with background jobs from a message queue: a cron job is triggered by time, while a queued job is triggered by an event, such as a user uploading a file. Cron jobs also have common pitfalls. They run in the server's time zone unless configured otherwise, their failures go unnoticed unless you log and monitor them, and scheduling the same job on several servers can make it run more than once, so jobs should be idempotent or protected by a lock.

Key takeaways

  • A cron job runs a command automatically on a time-based schedule.
  • Schedules have five fields: minute, hour, day of month, month, and day of week.
  • 0 2 * * * means every day at 2:00; */15 * * * * means every 15 minutes.
  • Cron syntax is also used by Kubernetes, CI/CD tools, and cloud schedulers.
  • Log and monitor jobs, mind time zones, and make jobs safe to run twice.

Example

Scheduling jobs in a crontabbash
# Edit the current user's crontab
crontab -e

# Format: minute hour day-of-month month day-of-week command
# Back up the database every day at 2:00 a.m.
0 2 * * * /usr/local/bin/backup-db.sh >> /var/log/backup.log 2>&1

# Delete temporary files older than an hour, every 15 minutes
*/15 * * * * find /tmp/myapp -type f -mmin +60 -delete

# Send a weekly report every Monday at 9:00 a.m.
0 9 * * 1 /usr/local/bin/send-report.sh

# List the jobs currently scheduled
crontab -l

Readers ask

What does * * * * * mean in cron?

Five asterisks mean every minute of every hour of every day. The fields are minute, hour, day of the month, month, and day of the week, and * means any value for that field.

What is the difference between cron and crontab?

Cron is the background service that runs scheduled jobs, while a crontab is the file that lists those jobs and their schedules. The crontab command is used to edit and view that file.

How do I run a cron job every 5 minutes?

Use the step syntax */5 * * * * followed by your command. The */5 in the minute field means every fifth minute, starting at minute 0.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings