Skip to content

Cron Job & Cron Expressions Guide ​

What is a Cron Job? ​

Cron is a job scheduler on Unix-like operating systems (such as Linux). Each job that cron runs automatically is called a cron job. Cron lets you schedule commands or scripts to run periodically at fixed times, dates, or intervals.

In FlashPanel, you create cron jobs for a server on the CronJob Scheduler page. This page helps you understand how to write cron expressions.

Advantages of using Cron Job ​

One of the hardships of system administrators is handling tasks day and night. Some tasks must run overnight or on weekends. Should you give up your free time every evening to run commands and scripts after hours, or stay up late to run backups and updates?

With Cron Job, you save a lot of time by scheduling repeated tasks, without having to remember and run recurring jobs manually.

Limitations when using Cron Job ​

Cron Jobs can only run commands at intervals of 1 minute or more. If you need to repeat a task more often than once a minute, cron cannot do it.

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 ​

For example, in the cron job line below, * * * * * is the Cron expression (run every minute) and the rest is the command:

* * * * * php /home/user/example.com/artisan schedule:run

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 the day of month or day of week field)
  • Note: This character only exists in some other schedulers (such as Quartz). Linux cron does not support ?; use * instead.

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 ​

  • Crontab Guru - Simple cron expression validator
  • Cron Expression Generator - Note: this tool generates Quartz expressions (6-7 fields); remove the seconds and year fields before using them with Linux cron

Command Line Testing ​

bash
# List the current user's crontab
crontab -l

# Edit the crontab (cron reports an error on save if the syntax is invalid)
crontab -e

WARNING

Do not use echo "..." | crontab - to test syntax: it overwrites the entire crontab of the current user.

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

# Discard normal output, keep only error messages
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
sudo grep -i cron /var/log/syslog
# or
sudo journalctl -u cron

Common Issues ​

  1. Path Problems: Always use absolute paths
  2. Environment Variables: Cron runs with very few environment variables, so define the ones you need
  3. Permissions: Ensure the user can execute the command
  4. Output Handling: Redirect output to a log file to make errors easier to check

For cron jobs created in FlashPanel, click View Log to see the output of their runs. See also CronJob troubleshooting.