What is Cron?

Cron is a powerful time-based job scheduler in Unix and Unix-like operating systems. It enables users to automate repetitive tasks by scheduling them to run at specific intervals.

Whether you need to back up your data, run maintenance scripts, or process large datasets, Cron is an essential tool for system administrators and developers.

This post will cover all you need to know as a cron beginner. In the final section, you’ll discover advanced tools that build on cron’s feature list and reach beyond cron’s limitations.


Why learn Cron?

Mastering Cron can dramatically boost your efficiency and productivity. By automating routine tasks, you free up time to focus on more essential aspects of your work.  Proficiency in Cron means you can ensure that tasks are executed consistently and reliably, minimizing the risk of human error.


Understanding Cron

How Cron works

Cron operates through a background process known as the cron daemon, or crond.

This friendly daemon runs continuously in the background and checks a list of scheduled tasks, executing them at the specified times. These scheduled tasks are known as Cron jobs.

Cron daemon (crond)

The cron daemon is the benevolent engine behind Cron’s functionality. It starts automatically when the system boots and remains running in the background, checking for tasks to execute.

The cron daemon reads configuration files, commonly known as crontabs, to determine which jobs to run and when to run them.

Cron jobs

Cron jobs are the tasks that you schedule using Cron. These jobs can be virtually anything: running a script, executing a command, or even simple tasks like sending an email.

By defining Cron jobs, you automate these tasks, ensuring they run at specified intervals without manual intervention.

Cron syntax and expressions

Understanding Cron syntax is a must for setting up (defining) effective Cron jobs. This syntax allows you to formulate actionable cron expressions.

A Cron expression consists of five fields: minute, hour, day of the month, month, and day of the week. Each field can contain specific values, ranges, or special characters to define the schedule.

Minute, hour, day of month, month, day of week:

  • Minute: Specifies the exact minute within an hour (0-59).
  • Hour: Specifies the hour of the day (0-23).
  • Day of Month: Specifies the day of the month (1-31).
  • Month: Specifies the month of the year (1-12).
  • Day of Week: Specifies the day of the week (0-7), where both 0 and 7 represent Sunday.

Special characters in Cron:

  • Asterisk (*): Represents all possible values for a field.
  • Comma (,): Separates multiple values.
  • Hyphen (-): Defines a range of values.
  • Slash (/): Specifies intervals.

Common Cron expressions

Here are some common Cron expressions to illustrate how you can use them:

  • 0 0 * * * – Runs a job every day at midnight.
  • */15 * * * * – Runs a job every 15 minutes.
  • 0 9 * * 1 – Runs a job every Monday at 9 AM.
  • 0 0 1 * * – Runs a job at midnight on the first day of every month.

Advanced cron scheduling and monitoring tools like Cron To Go offer simplified solutions like rate expressions for users who require straightforward scheduling options, and a handy online expression editor for users who need to verify complex expressions.

cron cheatsheet cron syntax


Setting up Cron jobs

Creating and editing crontabs

To schedule tasks using traditional Cron, you need to create and edit crontab files. Each user on a Unix-like system can have their own crontab, which is a simple text file containing a list of Cron jobs.

To edit your crontab, use the following command:

crontab -e

This command opens your crontab file in the default text editor. Here, you can add, modify, or remove Cron jobs. Each line in the crontab represents a separate Cron job, defined by a Cron expression followed by the command to be executed.

Here’s a well structured example of a crontab:

# Edit this file to introduce tasks to be run by cron.
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 7) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

# Run a backup script every day at midnight
0 0 * * * /usr/local/bin/backup.sh

# Clean up temporary files every Sunday at 3 AM
0 3 * * 0 /usr/local/bin/cleanup.sh

# Rotate logs at the top of every hour
0 * * * * /usr/local/bin/logrotate.sh

# Send weekly report every Monday at 6 AM
0 6 * * 1 /usr/local/bin/weekly_report.sh

# Run a custom Python script every 30 minutes
*/30 * * * * /usr/bin/python3 /path/to/script.py

# Run database maintenance at 2:15 AM on the 1st and 15th of every month
15 2 1,15 * * /usr/local/bin/db_maintenance.sh

# Check for system updates at 4 AM on the first Sunday of every month
0 4 1-7 * 0 /usr/local/bin/check_updates.sh

# Email logs to admin every Friday at 5 PM
0 17 * * 5 /usr/local/bin/email_logs.sh

Example Cron jobs

Here are some examples of common Cron jobs:

Daily backup at midnight:

0 0 * * * /usr/local/bin/backup.sh

Weekly cleanup:

0 3 * * 0 /usr/local/bin/cleanup.sh

Hourly log rotation:

0 * * * * /usr/local/bin/logrotate.sh

Managing crontabs

Cron offers several useful commands for managing crontabs:


List crontabs: View the current user's crontab entries.

crontab -l

Remove crontabs: Delete the current user's crontab.

crontab -r

Run and Monitor Scheduled Tasks on your Favorite Apps
Cron To Go simplifies the monitoring, alerting, and management of your cron jobs' performance, uptime, and status - ensuring seamless operation.
Try Cron To Go for free!

Monitoring and troubleshooting Cron jobs

The importance of monitoring Cron jobs

Monitoring Cron jobs is the only way to ensure they run as expected. Without proper monitoring, jobs can fail silently, meaning missed tasks, potential system issues, and much less happy time at work.

Regular monitoring helps detect failures early and ensures tasks are completed correctly and without ruining your flow.

Using logs for troubleshooting Cron

Cron jobs typically log their output and errors to system logs.

To view these logs, you can check the /var/log/syslog file on most Unix-like systems. Look for entries containing "CRON" to find relevant log messages.

Example: viewing Cron logs

grep CRON /var/log/syslog

Common Cron issues and solutions

  • Job fails to execute: Ensure the command path is correct and the script has executable permissions.
  • Environment variables: Cron jobs run in a limited environment. Specify the full path to executables and set necessary environment variables within the script.
  • Permissions issues: Ensure the user running the Cron job has the necessary permissions to execute the command.

The best thing you can do to mitigate common Cron issues is to use an advanced Cron scheduling and monitoring solution like Cron To Go, but we’ll cover that in the final section of this post.


Advanced Cron usage and tips

Using Cron expressions for precise scheduling

Cron expressions define the schedule on which a Cron job will run. They consist of five fields representing different time intervals:

  1. Minute (0-59)
  2. Hour (0-23)
  3. Day of the month (1-31)
  4. Month (1-12)
  5. Day of the week (0-6, where 0 represents Sunday)

For example:

30 2 * * 1 /usr/local/bin/weekly_report.sh

This job runs the weekly_report.sh script at 2:30 AM every Monday.

Special characters in Cron expressions

  • Asterisk (*): Represents all possible values. For example, * * * * * runs a job every minute.
  • Comma (,): Specifies a list of values. For instance, 0 0,12 * * * runs a job at midnight and noon every day.
  • Hyphen (-): Defines a range of values. For example, 0 0-6 * * * runs a job every hour from midnight to 6 AM.
  • Slash (/): Defines step values. For instance, */15 * * * * runs a job every 15 minutes.

How to do what cron can’t do—with Cron To Go

We’ve already mentioned how tasks like monitoring, troubleshooting, and automating are best done using an advanced tool like Cron To Go. There’s more to it than that, because as handy as cron is, it actually has numerous limitations.

Cron To Go is designed to do everything cron can do and more, overcoming most of the challenges of traditional cron. Consider the following additional features:

  • Cloud-native solution: Unlike traditional cron, which relies on individual server uptime, Cron To Go offers a cloud-native architecture that enhances task reliability and improves performance and scalability across your applications and operations.
  • Advanced monitoring and logging: Provides real-time notifications and highly detailed execution logs and histories, features that are not available with traditional cron. This advanced monitoring ensures tasks are completed reliably and any issues are quickly identified and addressed.
cron job log monitoring
cron job history view
  • CI/CD integration: Cron To Go seamlessly integrates with continuous integration and deployment workflows, facilitating better automation and efficiency in deployment processes—a major upgrade over traditional cron.
  • Rate and frequency expressions: Traditional cron is limited to standard time intervals, making it challenging to schedule tasks that don't align neatly with its constraints. Conversely, Cron To Go supports rate expressions and frequency settings, allowing jobs to be scheduled at specific intervals beyond the standard cron format. This includes the ability to schedule jobs as frequently as every few minutes or on a less typical cycle.
  • Timezone support: Cron To Go allows users to set the timezone for each cron job. For instance, if you need a job to execute daily at 7 AM Pacific Time, even with the shift to and from Daylight Saving Time, Cron To Go facilitates this with its wall-clock-time feature. This ensures tasks run on time, every time, without being constrained by the server's default timezone settings.
  • Automated job retries: Cron To Go automatically retries jobs if they fail due to temporary issues such as API timeouts or errors, a capability absent in traditional cron. This feature reduces the need for manual intervention and ensures higher reliability.
  • Simplified user experience: Offers a user-friendly interface that simplifies job creation and management, significantly improving over the often complex crontab setup required in traditional cron systems. This makes scheduling and managing jobs more accessible and less error-prone.
cron to go user interface
  • Business Continuity: In traditional cron setups, a daemon or server failure brings processes to a halt. On the other hand, Cron To Go is not a single point of failure, as it has high availability and scalability built-in to its architecture.
  • Better security: Cron To Go includes security features to protect your jobs, including user access control to define who can create, edit, or view Cron jobs, trigger-based notifications and webhooks with customisable settings, and audit logs to track changes and access for compliance and security audits.
cron job status notifications


cron webhook cron monitoring

These features collectively make Cron To Go a superior choice for modern cloud environments and complex scheduling needs, no matter the scale.

Conclusion

Understanding Cron and mastering its usage is a tried and tested way to automate tasks and maintain system efficiency.

However, while traditional Cron offers a solid foundation, its limitations can hinder productivity and reliability, especially at scale and in complex environments.

Cron To Go addresses these challenges with advanced scheduling, robust monitoring, automated retries, and a range of powerful features, making it a wonder-tool for developers and system administrators.

Start using Cron To Go today to experience the benefits of reliable, efficient, and hassle-free job scheduling.


Run and Monitor Scheduled Tasks on your Favorite Apps
Cron To Go simplifies the monitoring, alerting, and management of your cron jobs' performance, uptime, and status - ensuring seamless operation.
Try Cron To Go for free!

Frequently Asked Questions

What is Cron?

Cron is a time-based job scheduler in Unix-like operating systems that automates repetitive tasks by scheduling them to run at specific intervals.

How does Cron work?

Cron operates through a background process called the cron daemon, which continuously runs and checks for scheduled tasks, executing them at the specified times.

What are Cron jobs?

Cron jobs are the tasks scheduled using Cron. They can include running scripts, executing commands, or performing routine system maintenance.

What is a crontab?

A crontab is a configuration file that contains a list of Cron jobs. Each user on a Unix-like system can have their own crontab to manage their scheduled tasks.

How do you edit a crontab file?

You can edit a crontab file using the command crontab -e, which opens the crontab file in the default text editor for adding, modifying, or removing Cron jobs.

What is a Cron expression?

A Cron expression is a string consisting of five fields (minute, hour, day of the month, month, and day of the week) that defines the schedule on which a Cron job will run.

Why is monitoring Cron jobs important?

Monitoring Cron jobs is essential to ensure they run as expected, detect failures early, and avoid potential system issues due to missed tasks.

How can you view Cron job logs?

You can view Cron job logs by checking the /var/log/syslog file and looking for entries containing "CRON." This helps in troubleshooting and ensuring jobs run correctly.

What are the benefits of using Cron To Go?

Cron To Go enhances traditional Cron with features like real-time alerts, detailed logging, automatic retries, and advanced scheduling capabilities, ensuring reliable and efficient task execution.