Cron - The OG of Schedulers
The word Automation was a Buzz word a decade ago and probably still is before AI started buzzing across industries. When you think of Automation, some people think of Robots, and others think of Supply Chain Automation, but if you are a system admin, I know what you are thinking.
CRON JOBS!
What is a Cron Job?
For people unaware, a cron job is a time-based job scheduler in Unix-like operating systems. The cron daemon runs in the background and executes scheduled commands at specified intervals, based on the configurations set by the user. These configurations are defined in a file called the crontab (cron table), where users can specify what tasks to run and when.
cron is native to Unix-like Operating Systems & is not a part of Windows operating systems. Windows uses the Task Scheduler instead, which serves a similar purpose but operates within the Windows environment.
Before getting on with the background, and some examples, it might help to understand what a daemon program is in computing.
What is a daemon process
A daemon is a background process that runs continuously and performs specific tasks or provides services without direct user interaction. Daemons are typically started during the system boot process and continue running until the system shuts down. Some examples of a daemon process are -
- sshd (The OpenSSH server daemon that handles incoming SSH connections)
- httpd (The Apache HTTP server daemon that serves web pages to users) & yes, you guessed it right;
- cron (A daemon that schedules and executes recurring tasks based on user-defined schedules)
What is the Background of cron?
The name ‘cron’ is derived from the Greek word “chronos,” which means time. It was introduced in the early 1970s as part of Version 7 Unix by Ken Thompson and his colleagues at AT&T Bell Labs. The concept was developed to address the need for automating repetitive tasks, making system administration more efficient by allowing users to schedule commands or scripts to run at specified intervals [Wiki]. But it is now used across including developers who run ETL pipelines, ML Models, and especially if they do not have any access to orchestration tools.
The original version of cron was part of the Unix operating system and was made and designed to handle repetitive tasks. It provided a way for administrators and users to schedule jobs that would be executed automatically at defined times and intervals. Cron jobs are configured through crontab files, which list commands to be executed and the schedule for each command. There are typically two types of crontab files: user-specific crontabs and system-wide crontabs. User-specific crontabs are managed by individual users and allow them to set up their own scheduled tasks, while system-wide crontabs are managed by system administrators for global tasks.
cron has evolved with various Unix and Linux distributions incorporating their own versions or enhancements. While the core functionality has remained the same, different implementations have added features such as improved logging, error handling, and the ability to manage cron jobs through graphical interfaces.
Today, cron remains a default in Unix-like operating systems, including Linux, macOS, and others. It is widely used for tasks such as system maintenance, data backups, and automated reporting. Despite the advent of more advanced scheduling systems and task managers, cron’s simplicity and reliability have ensured its continued use.
Examples of Cron Job Configurations
cron can be configured for various times, days and hours. Before we get into some examples, it would be easier to know the syntax of cron. The Syntax is as follows;
IN HOUR DOM MON DOW CMD
Field | Description | Values Allowed |
---|---|---|
MIN | Minute | 0 to 59 |
HOUR | Hour | 0 to 23 |
DOM | Day of Month | 1-31 |
MON | Month | 1-12 |
DOW | Day Of Week | 0-6 |
CMD | Command | command to be executed |
Also, note that cron uses operator symbols which allow you to specify multiple values in a single field.
- Asterisk (*) - specifies all possible values for a field.
- comma (,) - specifies a list of values.
- Dash (-) - specifies a range of values.
- Separator (/) - specifies a step value.
Now, here are a few examples that show how it works;
-
Daily Backup at Midnight - To back up a directory every day at midnight, add the following line to your crontab:
0 0 * * * /usr/bin/rsync -av /home/user/data /backup/
This configuration means “At minute 0 of hour 0 (midnight) every day, execute the rsync command.”
-
Weekly System Update - If you want to update your system every Sunday at 3 AM, use the below:
0 3 * * 0 apt-get update && apt-get upgrade -y
-
Let’s say you want to execute a shell script every 12 hours Locally that triggers a Python script in an Ubuntu Environment. This is what your crontab will look like. This is very useful to note as these kinds of schedules can help you build ETL Pipelines and many other applications locally without relying on any cloud-based schedulers.
0 */12 * * * /home/PythonScriptTrigger.sh
cron requires the absolute path to be mentioned and not relative paths.
Now a natural question would be to know how to start crontab or a cronjob scheduler in Unix-like systems.
To start using crontab in Linux-based Systems, you need to interact with the cron service and configure your cron jobs. Here’s a step-by-step process to follow. This is the process for Ubuntu, but the same process can be followed in other versions as well.
- Check if cron is installed by entering ‘which cron’ in the terminal. The command should return a path which means it’s installed, otherwise, cron has to be installed.
- Next, check if the cron service is running by entering “service cron status”. If it’s not running, you can start it with “sudo service cron start”. This is assuming your systems init system and service manager is “SysVinit”.
- You can now edit the crontab file and add your cron jobs. You can use the commands as mentioned above, or any other command that suits your requirement.
- Save and exit the file.
- You can also verify the cron job by running “crontab -l” which should display the cron job you scheduled.
This is how your crontab file should look like in an Ubuntu system.
Also, cron jobs do not start by default in some machines and have to be configured to start at boot in case you wish to use cron frequently. In such cases, you can use the “sudo systemctl enable cron” command to start cron every time you boot your system or you can also start cron manually by “sudo systemctl start cron”. If your service manager & init system is “SysVinit”, then you can use “sudo service cron enable” to start cron on reboot and “sudo service cron start” to start cron manually.
A similar setup as mentioned above for Linux-based systems can be followed for Mac as well. There are many more options to write cron based on the schedule you want to run them for & the above are some examples. You can also get the cron cheatsheet here.
In case you are running a script, make sure the user executing the cron job has the right permissions.
Where is cron used?
Cron is used extensively across various domains where scheduling and automation of repetitive tasks are required. Here are some common use cases:
-
System Administration:
-
Backups: Automating regular backups of files, databases, or entire systems to ensure data is not lost and can be restored if needed.
-
System Updates: Scheduling updates and patches to ensure that systems are up-to-date with the latest security fixes and improvements.
-
Monitoring: Running periodic health checks on system resources, disk usage, and services to ensure everything is functioning correctly.
-
-
Web Hosting:
-
Data Syncing: Regularly synchronizing data between servers, such as copying files between a web server and a backup server.
-
Website Maintenance: Performing tasks like clearing caches, generating sitemaps, or checking for broken links on a regular schedule.
-
-
Development and Testing:
-
Automated Builds: Running build scripts or tests automatically at specified times, such as nightly builds or continuous integration (CI) processes.
-
Deployment: Automating the deployment of applications or updates at scheduled times to minimize disruptions.
-
-
Business Operations:
-
Email Alerts: Sending periodic email notifications or alerts, such as reminders or status updates.
-
Data Collection: Scheduling scripts to collect and process data from various sources, such as web scraping or API calls.
-
-
Personal Use:
-
Reminders: Setting up personal reminders or notifications for tasks like routine maintenance, reminders, or personal projects.
-
Automated Tasks: Running personal scripts or programs on a regular schedule, such as organizing files or performing routine clean-ups.
-
Cron’s accessibility & popularity is a testament to its effectiveness in automating tasks and helping users, developers and administrators manage their systems & processes more efficiently & with ease.