As developers, we're often required to perform repetitive operations to ensure our systems are working as expected. These tasks include taking database backups, deleting temporary files, and performing other system maintenance tasks.
Two of the most common options for automating these processes on a Linux-based computer system are cron and anacon. In this article we'll introduce cron and anacron, explore their similarities and differences, and walk through how to implement each. We'll also discuss some of their limitations and a simpler solution for scheduling internal operations: Airplane.
In most Linux systems, the cron daemon handles the majority of system backups, software upgrades, and other scheduled operations. It's great for executing scheduled activities on systems that operate 24 hours a day, seven days a week — such as servers.
The cron daemon works in the background. Every minute, it wakes up and checks to see whether someone has scheduled a job for that minute. If this is the case, it executes the job. Otherwise, it remains idle. The cron runs shell scripts or basic bash commands saved in a file.
Anacron is a service that runs commands regularly with a frequency set in days. One of the biggest differences between cron and anacron is that anacron considers that the computer is not always on. Anacron works well when running daily, weekly, and monthly scheduled jobs on machines that will not run 24-7, such as laptops or desktops.
For example, say we have a job that takes a backup and it's scheduled to run daily at 5AM when we're asleep. It's pretty likely that our laptop will be off at that time. If your backup script is run by cron, it won't be executed when your laptop is off. If we use anacron, the next time we power on our laptop, we'll see that the script has been executed.
Cron versus anacron
- Cron is a daemon that executes scheduled tasks on a system that is always running.
- Cron does not execute a scheduled job if the system is not running.
- With cron, you can schedule tasks to run every minute.
- Anacron is not a daemon and relies on other methods in Linux to execute scheduled tasks.
- Anacron is for systems that are usually turned off when not in use, such as personal computers.
- Anacron can only be run daily, monthly, or weekly (cannot be run by the minute).
- The main configuration file,
/etc/anacrontab, contains a list of scheduled jobs. Anacron examines each job to see whether it was completed within the specified time frame. If not, the job runs and the timestamps are saved in the
/var/spool/anacronfile. As cron does not perform these actions, this might be a deciding factor for organizations deciding between cron or anacron.
Cron and anacron are both used for automating scheduled processes on a Linux-based computer system.
Cron lets us run jobs at scheduled times, dates, and intervals. The first step in implementing cron is to use the root user in the terminal to edit the
etc/crontab configuration file for system-wide jobs:
Next, we execute the
crontab -e command in the same terminal to alter the crontab file.
If we are editing the crontab file for the first time, it asks us to use a text editor to open the crontab file. As shown in the screen capture, we can select 1 for
nano, 2 for
vim.tiny, 3 for
emacs, and 4 for
Selecting a text editor opens the crontab file.
In the next step, we create a cron job on the last line in the crontab file to execute the abc.sh file every minute.
* * * * * /bin/sh /home/username/Desktop/abc.sh
The syntax to add the cron job is
* * * * * options. Here,
options can either be a standalone command or a script file. Also, the first asterisk (*) means minutes (0 to 59), the second means hours (0 to 23), the third means the day of the month (1 to 31), the fourth means months (1 to 12), and the fifth means the day of the week (0 to 7).
After typing the command, press Ctrl + X, then type Y and press Enter to save the crontab file. Then a message displays similar to "installing new crontab."
Next, we create a shell script called
abc.sh that runs every minute as specified in the crontab file.
Inside that file, we write a command that prints the current date and time in UTC format to a text file called
logfile in the Desktop folder:
echo "'date -u'" >> /home/username/Desktop/log file
We can list cron jobs using the command
As a result of our cron job, a file named
logfile containing the current date and time prints every minute.
It's worth noting that removing a job from the crontab stops execution. If the server is not running, the cron job does not execute, and we might miss a critical task. That’s where anacron comes into the picture.
To start let's recall that anacron and cron don't run at the same granularity. Anacron can only be run daily, weekly, or monthly, while cron can be set to run every minute.
The first step in implementing anacron is to use a root user to edit the
/etc/anacrontab configuration file.
Next, we execute this command to open the anacron config file:
A typical anacrontab file looks like this:
To execute a
sample.sh script daily after the system starts using anacron, we add this command:
1 0 anacron.testjob /bin/bash /home/sample.sh
Here, the first argument in the command, 1, means it is a daily job. For weekly jobs, we specify 7 and 30 for monthly.
The second argument, 0, is system wait. The third argument is the job name, which can be set to anything. Lastly, the fourth argument can either be a command or shell script file to execute.
After defining the job, we create the shell script file inside the home directory.
Then, we execute the following command to create a sample.sh file:
Inside the file, we paste the following command, which prints the message every day as defined when we created the job.
echo "This is an anacron job">> /home/Hello.txt
As a result, we have created
Similar to cron, removing the job from the anacrontab will stop execution.
The pain points of using cron and anacron
There are many pain points when using either cron or anacron to carry out timed jobs. A few notable ones include:
- Lack of graphical user interface (GUI) — Without a GUI, there is no easy way to determine whether jobs are running. The only way to watch cron jobs is via log files and emails. This can directly impact usability and maintainability if we have a large application with many jobs.
- No fault tolerance — Cron and anacron do not have any built-in functionality to respond to failures. When a cron or anacron job fails, we'll need to re-run the scheduled task manually, which causes interruptions and can be time-consuming and frustrating depending on the number of jobs.
- Performance issues — Sometimes, a poorly configured cron job can use more memory, impacting the performance of the server or basic system operations. This memory leak could also result in server freeze, and thus downtime. Cron and anacron may fail for other reasons, such as overlapping jobs. Because of this, you'll likely need to build out monitoring logic to ensure cron and anacron jobs are working properly, which may require more human resources.
Airplane schedules are a serverless alternative that can help eliminate many of the pain points experienced while using cron or anacron to carry out scheduled operations. Using Airplane, you can manage, edit, and create schedules centrally. Airplane also supports audit logs, permissions, and features a direct integration with Slack or email to send notifications if jobs fail. You can sign up for a free account to try out Airplane today.