Cron Job
- Pronunciation
- KRAHN 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
# 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 -lReaders 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
- LinuxDevOps & Cloud, p. 33Linux is an open-source operating system kernel that powers most servers, cloud platforms, containers, and Android phones, usually packaged as a distribution.
- DevOpsDevOps & Cloud, p. 14DevOps is a set of practices and a culture that brings software development and IT operations together to deliver software faster and more reliably.
- KubernetesDevOps & Cloud, p. 32Kubernetes is an open-source system that automates deploying, scaling, and managing containerized applications across a cluster of machines.
- ServerlessDevOps & Cloud, p. 47Serverless is a cloud model in which the provider runs your code on demand, manages all the servers, scales automatically, and bills only for actual use.
- Message QueueBackend & APIs, p. 29A message queue is a component that stores messages from one service until another is ready to process them, so parts of a system can work asynchronously.
- IdempotencyBackend & APIs, p. 24Idempotency is the property of an operation that produces the same result whether it runs once or many times, so accidentally repeating a request is safe.
Spotted a mistake or something missing on this page?Suggest an edit