Cron Jobs Explained: How to Schedule Tasks with Crontab Syntax

Learn how cron and crontab work on Linux, how to read the five time fields, and how to schedule backups and scripts without common mistakes.

Share on Linkedin Share on WhatsApp

Estimated reading time: 7 minutes

Article image Cron Jobs Explained: How to Schedule Tasks with Crontab Syntax

Backups that run every night, log files that get cleaned up every week, reports that are emailed every Monday morning: behind many of these routine jobs on Linux and other Unix-like systems is a small, reliable tool called cron. Learning how cron works and how to read its scheduling syntax is a basic skill for developers, system administrators and anyone who manages a server. In this guide you will learn what cron is, how the crontab format works, how to write and test your own scheduled tasks, and which pitfalls catch beginners most often.

What is cron?

Cron is a time-based job scheduler. It runs in the background as a service (a daemon, often called cron or crond) and checks, once a minute, whether any scheduled command should be executed. The schedules are stored in files called crontabs (short for “cron tables”). Each line in a crontab describes one job: when it should run and what command it should execute.

A scheduled task defined this way is commonly called a cron job. The same scheduling syntax has become so popular that many other tools, including CI/CD pipelines, cloud schedulers and container orchestrators, use cron expressions (sometimes with small variations) to define recurring tasks.

Managing your crontab

Each user can have a personal crontab. You manage it with the crontab command:

  • crontab -e opens your crontab in a text editor so you can add or change jobs.
  • crontab -l lists your current jobs.
  • crontab -r removes your entire crontab. Be careful: on many systems it does not ask for confirmation, and it sits right next to -e on the keyboard.

Editing through crontab -e is recommended over editing files directly, because the command checks the syntax and tells the cron service to reload the schedule.

The five time fields

A standard crontab line has five time fields followed by the command:

# ┌───── minute (0-59)
# │ ┌───── hour (0-23)
# │ │ ┌───── day of month (1-31)
# │ │ │ ┌───── month (1-12)
# │ │ │ │ ┌───── day of week (0-7, where 0 and 7 are Sunday)
# │ │ │ │ │
  * * * * *  command-to-run

An asterisk means “every possible value” for that field. So * * * * * runs the command every minute of every hour of every day.

Special characters

  • Comma (,) lists values: 1,15 in the day-of-month field means the 1st and the 15th.
  • Hyphen (-) defines a range: 1-5 in the day-of-week field means Monday to Friday.
  • Slash (/) defines steps: */15 in the minute field means every 15 minutes (0, 15, 30, 45).

Common schedule examples

ExpressionMeaning
0 * * * *At minute 0 of every hour
*/10 * * * *Every 10 minutes
30 2 * * *Every day at 02:30
0 9 * * 1-5At 09:00, Monday to Friday
0 0 1 * *At midnight on the first day of every month
15 18 * * 0At 18:15 every Sunday
0 6,18 * * *At 06:00 and 18:00 every day

Many cron implementations also accept shortcuts such as @daily, @weekly, @monthly and @reboot (run once when the system starts). They are convenient, but check your system’s documentation, because support can vary.

Writing your first cron job

Suppose you have a script that backs up a folder and you want it to run every night at 1:00 AM. A good practice is to follow these steps:

  1. Test the script manually first. Run it from the terminal and make sure it works.
  2. Make it executable with chmod +x /home/user/scripts/backup.sh.
  3. Open the crontab with crontab -e.
  4. Add the job using absolute paths and redirect the output to a log file.
0 1 * * * /home/user/scripts/backup.sh >> /home/user/logs/backup.log 2>&1

The >> appends standard output to the log file, and 2>&1 sends error messages to the same place. Without this redirection, depending on the system configuration, the output may be emailed to the local user or simply lost, which makes troubleshooting much harder.

Common pitfalls and how to avoid them

1. A minimal environment

Cron does not load your full interactive shell environment. Variables you set in your profile, and even the PATH you are used to, may not exist when the job runs. That is why a script that works in the terminal can fail under cron. Use absolute paths for commands and files, or define the variables you need at the top of the crontab or inside the script.

2. Time zones

Cron uses the system’s time zone. On cloud servers this is frequently UTC, which may differ from your local time. Check it with the date command before choosing an hour, and remember that daylight saving changes can cause a job to be skipped or run twice around the transition, depending on the implementation.

3. The percent sign

In a crontab line, the % character has a special meaning (it is converted to a newline). A command like date +%Y-%m-%d must be written with escaped percent signs, as date +\%Y-\%m-\%d, or moved into a script.

4. Day of month and day of week together

When both the day-of-month and day-of-week fields are restricted (neither is *), classic cron runs the job when either condition matches, not only when both do. For example, 0 0 13 * 5 runs on the 13th of every month and on every Friday, not only on Friday the 13th.

5. Overlapping runs

If a job takes longer than its interval, a new copy may start before the previous one finishes. For long tasks, use a lock mechanism, such as the flock utility available on many Linux distributions, to make sure only one instance runs at a time.

Checking that your job ran

To confirm a cron job is working, check the log file you configured. You can also look at the system logs: depending on the distribution, cron activity appears in files such as /var/log/syslog or /var/log/cron, or can be viewed with journalctl on systems that use systemd. While testing, it helps to temporarily schedule the job a few minutes ahead and watch what happens.

Conclusion

Cron is simple, dependable and available on almost every Linux server. Once you understand the five time fields, the special characters and a few common traps, such as the minimal environment and time zones, you can automate backups, cleanups and reports with confidence. If you want to keep building practical skills with Linux, the command line and other everyday tools, explore the free Developer and IT Tools courses on Cursa.

NTFS, exFAT, FAT32 and APFS: Choosing the Right File System for a Drive

Understand what a file system does and how NTFS, exFAT, FAT32, APFS and ext4 differ, so you can format drives without losing compatibility.

Text Encoding Explained: ASCII, Unicode and Why You Sometimes See Strange Symbols

Learn how computers store text, what ASCII and Unicode actually are, why UTF-8 became the standard, and how to fix files that display garbled characters.

Idempotency in APIs: Why Retrying a Request Should Be Safe

Learn what idempotency means in backend development, which HTTP methods provide it, and how idempotency keys prevent duplicate operations.

What Is a CDN? How Content Delivery Networks Make Websites Fast

Learn what a CDN is, how edge caching and cache headers work, what a cache hit means, and when a CDN helps — or does not.

Semantic Versioning Explained: What a Number Like 2.4.1 Actually Tells You

MAJOR.MINOR.PATCH is a promise, not decoration. Learn to read version numbers and understand dependency range symbols.

What Is a Virtual Machine? Virtualization Explained for Beginners

Learn what a virtual machine is, how hypervisors work, how VMs differ from containers, and when to use each one.

How HTTPS Works: Certificates, the TLS Handshake and What the Padlock Really Means

A beginner-friendly walkthrough of HTTPS: what TLS certificates prove, how the handshake works, and what the browser padlock does not guarantee.

Big O Notation Explained: How to Talk About Code Efficiency

A beginner-friendly guide to Big O notation: what it measures, the most common complexity classes, and how to reason about the cost of your code.