Skip to content

Cron Job & Cron Expressions Guide

What is Cron Job?

The cron command line utility, also known as cron job, is a job scheduler on Unix-like operating systems. Users set up and maintain software environments that use cron to schedule jobs to run periodically at fixed times, dates, or intervals.

Advantages of using Cron Job

One of the hardships of sysadmins is probably having to do tasks day and night. Some tasks need to be done overnight, or must be done on weekends. So we have to spend our free time every night to run commands and scripts after hours? Or do you always have to stay up at night to run backups or updates?

Thanks to Cron Job, you will save a large amount of time when scheduling repeated tasks, without having to try to remember and create recurring jobs over and over again.

Limitations when using Cron Job

Cron Jobs can only execute commands in cycles of 1 minute or more. In case you want to perform repetitive tasks in cycles less than 1 minute, it will not be possible.

Cron Job Components

A cron job consists of two main components:

  • Cron Expression: Indicates the job repetition cycle
  • Command: The command that will be executed

Understanding Cron Expressions

As you can see in the image above, * * * * * is the Cron expression.

Basic Structure

A cron expression consists of 5 fields separated by spaces:

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of the month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday)
│ │ │ │ │
│ │ │ │ │
* * * * *

Field Values and Ranges

FieldRangeDescription
Minute0-59Which minute of the hour
Hour0-23Which hour of the day (24-hour format)
Day of Month1-31Which day of the month
Month1-12Which month of the year (or JAN-DEC)
Day of Week0-6Which day of the week (0=Sunday, or SUN-SAT)

Special Characters

Asterisk (*)

  • Meaning: All values within the allowed range
  • Examples:
    • * in minute field = every minute
    • * in hour field = every hour
    • * in day field = every day

Comma (,)

  • Meaning: List separator for multiple values
  • Examples:
    • 7,19 in hour field = 7 AM and 7 PM
    • 1,15 in day field = 1st and 15th of the month
    • MON,WED,FRI in day of week field = Monday, Wednesday, Friday

Hyphen (-)

  • Meaning: Range of values
  • Examples:
    • 7-19 in hour field = from 7 AM to 7 PM
    • 1-5 in day of week field = Monday to Friday
    • JAN-MAR in month field = January to March

Slash (/)

  • Meaning: Step values (intervals)
  • Examples:
    • */5 in minute field = every 5 minutes
    • 0-20/2 in hour field = every 2 hours from midnight to 8 PM
    • */3 in month field = every 3 months

Question Mark (?)

  • Meaning: No specific value (used in day of month or day of week)
  • Usage: When you want to trigger on one but not the other

Month and Day Names

You can use names instead of numbers:

Months: JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC

Days: SUN, MON, TUE, WED, THU, FRI, SAT

Special Strings

Instead of the five fields, you can use these special strings:

StringMeaningEquivalent
@yearly or @annuallyRun once a year0 0 1 1 *
@monthlyRun once a month0 0 1 * *
@weeklyRun once a week0 0 * * 0
@daily or @midnightRun once a day0 0 * * *
@hourlyRun once an hour0 * * * *
@rebootRun at startupN/A

Common Cron Expression Examples

Basic Timing

ExpressionDescription
* * * * *Every minute
0 * * * *Every hour (at minute 0)
0 0 * * *Every day at midnight
0 0 * * 0Every Sunday at midnight
0 0 1 * *First day of every month at midnight
0 0 1 1 *Every January 1st at midnight

Specific Times

ExpressionDescription
30 2 * * *Every day at 2:30 AM
0 9 * * 1-5Every weekday at 9:00 AM
0 22 * * 1-5Every weekday at 10:00 PM
0 0,12 * * *Every day at midnight and noon
15 2,6,10,14,18,22 * * *Every day at 2:15, 6:15, 10:15, 14:15, 18:15, 22:15

Intervals

ExpressionDescription
*/5 * * * *Every 5 minutes
0 */2 * * *Every 2 hours
0 0 */3 * *Every 3 days
0 0 1 */2 *Every 2 months on the 1st
0 9-17 * * 1-5Every hour from 9 AM to 5 PM, Monday to Friday

Advanced Examples

ExpressionDescription
23 0-20/2 * * *At 23 minutes past every 2nd hour from midnight to 8 PM
0 4 8-14 * *At 4:00 AM on every day-of-month from 8 through 14
0 0 1,15 * 3At midnight on the 1st and 15th of every month, and every Wednesday
5 0 * 8 *At 12:05 AM every day in August
15 14 1 * *At 2:15 PM on the first day of every month

Real-World Use Cases

ExpressionDescriptionUse Case
0 2 * * *Daily at 2:00 AMDatabase backups
0 0 * * 0Weekly on Sunday midnightWeekly reports
*/15 * * * *Every 15 minutesSystem health checks
0 0 1 * *Monthly on 1st at midnightMonthly billing
0 9 * * 1Every Monday at 9:00 AMWeekly team meetings
0 */6 * * *Every 6 hoursLog rotation
30 2 * * 1-5Weekdays at 2:30 AMBusiness day maintenance

Testing Your Cron Expressions

Online Tools

Command Line Testing

bash
# Test if your cron syntax is valid
echo "0 2 * * * /path/to/script.sh" | crontab -

# List current crontab
crontab -l

# Edit crontab
crontab -e

Best Practices

1. Be Specific About Timing

bash
# Good: Specific time
0 3 * * * /backup.sh

# Avoid: Too frequent without purpose
* * * * * /script.sh

2. Use Absolute Paths

bash
# Good
0 2 * * * /usr/bin/php /var/www/app/backup.php

# Bad
0 2 * * * php backup.php

3. Handle Output Properly

bash
# Redirect output to avoid email spam
0 2 * * * /backup.sh > /var/log/backup.log 2>&1

# Send only errors to email
0 2 * * * /backup.sh > /dev/null

4. Consider Server Load

  • Avoid scheduling all jobs at the same time (e.g., all at midnight)
  • Stagger similar jobs across different times
  • Use random delays for distributed systems

Common Pitfalls

1. Day of Month vs Day of Week

When both day of month and day of week are specified (not *), the job runs when EITHER condition is met:

bash
# This runs on the 15th of every month AND every Friday
0 0 15 * 5

# To run only on Friday the 15th, use:
0 0 15 * * [ $(date +\%u) -eq 5 ] && /script.sh

2. Month Boundaries

bash
# Be careful with day 31 - doesn't exist in all months
0 0 31 * * /script.sh  # Won't run in February, April, June, etc.

3. Timezone Considerations

  • Cron runs in the system timezone
  • Consider daylight saving time changes
  • Document timezone assumptions

Practice Exercises

Try to figure out what these expressions mean:

  1. 0 0,12 1 */2 * - ?
  2. 0 4 8-14 * * - ?
  3. 0 0 1,15 * 3 - ?
  4. 5 0 * 8 * - ?
  5. 15 14 1 * * - ?
  6. */10 9-17 * * 1-5 - ?
  7. 0 2 * * 1,3,5 - ?
  8. 30 */3 * * * - ?

Answers:

  1. At midnight and noon on the 1st day of every 2nd month
  2. At 4:00 AM on every day from 8th to 14th of every month
  3. At midnight on the 1st and 15th of every month, and every Wednesday
  4. At 12:05 AM every day in August
  5. At 2:15 PM on the 1st day of every month
  6. Every 10 minutes from 9 AM to 5 PM, Monday to Friday
  7. At 2:00 AM on Monday, Wednesday, and Friday
  8. At 30 minutes past every 3rd hour

Troubleshooting Cron Jobs

Check if Cron is Running

bash
sudo systemctl status cron
# or
sudo service cron status

View Cron Logs

bash
# On most systems
sudo tail -f /var/log/cron

# On Ubuntu/Debian
sudo tail -f /var/log/syslog | grep cron

Common Issues

  1. Path Problems: Always use absolute paths
  2. Environment Variables: Cron has minimal environment
  3. Permissions: Ensure the user can execute the command
  4. Output Handling: Redirect output to prevent issues

This comprehensive guide should help you master cron expressions and create effective scheduled tasks!