Automating repetitive tasks is essential for efficient system administration. Cron is the go-to scheduler on Unix-like systems, allowing you to run commands and scripts at specified times without manual intervention.

What is Cron?

Cron is a time-based job scheduler in Unix-like operating systems. It enables users to schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals. Common use cases include:

  • Database backups
  • Log rotation and cleanup
  • Sending scheduled emails or reports
  • System maintenance tasks
  • Syncing files between servers

Cron Syntax Explained

A cron expression consists of five fields (plus the command to execute):

┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12)
│ │ │ │ ┌───────────── day of week (0-6, Sunday=0)
│ │ │ │ │
* * * * * command to execute

Special Characters

Character Meaning Example
*Any value* in hour = every hour
,Value list separator1,15 = 1st and 15th
-Range of values1-5 = 1 through 5
/Step values*/15 = every 15 units

Common Cron Expressions

Time-Based Schedules

Expression Description
* * * * *Every minute
*/5 * * * *Every 5 minutes
0 * * * *Every hour (at minute 0)
0 0 * * *Daily at midnight
0 0 * * 0Weekly on Sunday at midnight
0 0 1 * *Monthly on the 1st at midnight
0 0 1 1 *Yearly on January 1st

Business Hours Examples

Expression Description
0 9-17 * * 1-5Every hour 9am-5pm, Mon-Fri
30 8 * * 1-58:30am weekdays
0 */2 * * *Every 2 hours

Pro Tip

Use our Cron Generator to build and validate expressions without memorizing the syntax.

Managing Your Crontab

Basic Commands

# Edit your crontab
crontab -e

# List current cron jobs
crontab -l

# Remove all cron jobs
crontab -r

# Edit another user's crontab (requires root)
crontab -u username -e

Crontab File Locations

  • User crontabs: /var/spool/cron/crontabs/
  • System crontab: /etc/crontab
  • Cron directories: /etc/cron.d/, /etc/cron.daily/, /etc/cron.hourly/

Using Our Cron Generator

Our Cron Expression Generator helps you create valid cron expressions:

  1. Select the schedule type (every minute, hourly, daily, etc.)
  2. Customize specific fields as needed
  3. Preview the resulting expression
  4. See human-readable description of when it will run
  5. Copy the expression for your crontab

Best Practices

Writing Robust Cron Jobs

  1. Use absolute paths: Cron has a minimal PATH environment
    /usr/bin/php /var/www/script.php
  2. Redirect output: Capture both stdout and stderr
    0 * * * * /path/script.sh >> /var/log/script.log 2>&1
  3. Use lock files: Prevent overlapping runs
    0 * * * * flock -n /tmp/script.lock /path/script.sh
  4. Set environment variables: Define them at the top of your crontab

Security Considerations

  • Avoid storing sensitive credentials in cron entries
  • Use environment files or secret managers
  • Limit cron access with /etc/cron.allow and /etc/cron.deny
  • Run jobs with minimal required privileges

Troubleshooting Cron Jobs

Common Issues

Job Not Running

  • Check if cron service is running: systemctl status cron
  • Verify syntax with our Cron Generator
  • Check system logs: grep CRON /var/log/syslog

Permission Denied

  • Ensure the script is executable: chmod +x script.sh
  • Check file ownership and permissions
  • Verify the user has cron access

Different Behavior Than Manual Execution

  • Cron uses a limited environment
  • Source your profile or define variables in crontab
  • Use absolute paths for all commands and files

Important

Always test your cron jobs manually first. Run the exact command that will be in your crontab to ensure it works correctly.

Conclusion

Cron is an incredibly powerful tool for automation. Master the syntax and best practices, and you'll save countless hours on repetitive tasks. Use our Cron Expression Generator to quickly create and validate your cron schedules.