As a system administrator, you often need to run a particular task repeatedly and at a particular time without any user intervention. Tasks such as backups, cleaning temporary files, and other system maintenance fall under this category. Fortunately, there are several different ways to automate these tasks on a Linux system. In this article we will discuss using cron and anacron to automate tasks. We will outline their differences and the basics of how to schedule jobs with each.
Cron vs. Anacron
Both Cron and Anacron automatically run reoccurring jobs that at a scheduled time. Cron runs the scheduled jobs at a very specific interval, but only if the system is running at that moment. However, Anacron runs the scheduled job even if the computer is off at that moment. It runs those missed jobs once you turn on the system.
NOTE: For one time jobs (as opposed to reoccurring jobs) see the at command.
Cron jobs are suitable for servers that run 24/7 continuously, while the Anacron is better suited for desktops and laptops that are powered off when not in use. Furthermore, cron is more granular and can run jobs at a very specific time while anacron has a minimum time interval of once per day (daily).
Cron
- Used to execute scheduled commands
- Assumes the system is continuously running.
- If system is not running during the time the jobs is scheduled, it will not run.
- Can schedule jobs down to the precise minute
- Universally available on all Linux systems
- Cron is a daemon
Anacron
- Used to execute commands periodically
- Suitable for systems that are often powered down when not in use (Laptops, workstations, etc..)
- Jobs will run if it hasn't been executed in the set amount of time.
- Minimum time frame is 1 day
- Anacron is not a daemon and relies on other methods to run
The Cron Daemon
Crond is a daemon in Linux OS used for automation of recurring jobs. It is a background process that works by waking up every minute to check whether there is any job scheduled at that minute. If so, it runs the job otherwise stays inactive. The cron daemon checks for scheduled jobs the /etc/crontab file, /var/spool/cron, and /etc/cron.d directories and executes them if the scheduled time matches the system time.
Scheduling a Job With Cron
In cron you can specify a job to run at a specific time, date, and intervals with a minute as the minimum unit of time.
A root user can configure system wide cron jobs by editing the /etc/crontab file with a text editor like nano or vi:
$ nano /etc/crontab
Non-root users can define their cron jobs in their own crontab file using this:
$ crontab -e
NOTE: The root user can also run crontab -e
to set root user specific jobs.
Basic Cron Job Syntax
In order to schedule a cron job, add an entry in the crontab file using the following syntax:
* * * * * command/script
Each of the *
in the above syntax corresponds to the following:
Minute (0-59) Hour (0-24) Day of Month (1-31) Month of Year (1-12) Day of Week (0-6)
An asterisk means all values. Let's take a look at an example.
Example:
The following entry will set a backup job to run (by running backup_script.sh) at the 3:00 am on the 30th day of every month.
0 3 30 * * backup_script.sh
You can view the crontab file for the current user with the following command:
$ crontab –l
To view system defined cron jobs, use the cat command as follows:
$ cat /etc/crontab
The cron daemon does not run the scheduled job if the system is off at the specified time interval. It expects the system to be running all the time. For instance, you have scheduled a backup job at 8 o'clock and your system was off at that time, the job will not execute. Therefore, cron is mostly suited for servers which run continuously 24/7 and do not shut down. It is not suitable for use in desktops and laptops which are routinely powered down when not in use.
The Anacron Utility
Unlike cron, anacron does not expect the system to be continuously running all the time and therefore is better suited for desktop and laptops.
When anacron runs, it checks if the job was last executed in the specified amount of time. If it has not been because the system was off (or any other reason), it will run the job.
For example, let's assume you set a backup job to run daily in anacron. If you shut your system down for 3 days, at the next boot anacron will recognize that that job hasn't been run in the last day and execute the job.
Anacron is not a daemon and therefore relies on other mechanisms to run. On some systems it runs from a systemd timer unit, while other times it relies on the cron daemon itself.
Scheduling with Anacron
Anacron does not work with the exact time and date. Instead, it works with a period which is specified in days. These days can be represented in several formats including daily, monthly, and weekly.
The /etc/anacrontab
is the main configuration file where the schedule jobs are listed. For each job, anacron checks whether the job has been executed in the defined period. If not, it runs the job and stores the timestamps of the job in the /var/spool/anacron
file.
You can edit the anacrontab file with sudo access or as the root user.
$ nano /etc/anacrontab
Creating a User Specific Anacron Job
Non-privileged users (without sudo or root access) can create their own, user specific anacron instances.
First, create a .anacron
directory inside your home folder with two sub-directories (etc and spool) to match the system configuration.
$ mkdir -p ~/.anacron/{etc,spool}
Then create a new file to hold your user specific anacron jobs, like so:
$ echo $SHELL >> ~/.anacron/etc/anacrontab
$ echo $PATH >> ~/.anacron/etc/anacrontab
Then add the following cron job using crontab -e
command to ensure your anacron runs hourly:
@hourly /usr/sbin/anacron -s -t $HOME/.anacron/etc/anacrontab -S $HOME/.anacron/spool
Basic Anacron Job Syntax
The jobs in the anacrontab file are listed in the following format:
period delay job-ID command/script
Where:
- Period: In “days” or as @daily, @weekly, and @monthly.
- Delay: Only in minutes. Anacron waits for this time before executing the job at specified period.
- Job-ID: A unique identifier for the scheduled job
Example:
The following entry in the /etc/anacrontab
or ~/.anacron/etc/anacrontab
file will execute a backup script daily and with a delay time of 30 minutes.
1 30 daily_backup backup_script.sh
The other jobs listed in this file are the system defined entries that execute all the jobs in /etc/cron.*
directories.
Although Anacron runs automatically at boot, but you can also run it manually as follows:
$ sudo anacron
To view the directory where the time stamps for all the files are stored, you can run the following command:
kbuzdar@Linux-debian:~$ ls -l /var/spool/anacron
total 16
-rw------- 1 root root 9 May 13 19:36 cron.daily
-rw------- 1 root root 9 May 13 19:36 cron.monthly
-rw------- 1 root root 9 May 13 19:36 cron.weekly
-rw------- 1 root root 9 May 13 19:36 daily_backup
To force the execution of jobs, you can use -f
switch. The -d
switch is used here to run the jobs in the foreground.
kbuzdar@Linux-debian:~$ sudo anacron -f -d
Anacron 2.3 started on 2020-05-13
Will run job `cron.daily' in 1 min.
Will run job `cron.weekly' in 10 min.
Will run job `cron.monthly' in 15 min.
Will run job `daily_backup' in 30 min.
Job `cron.daily' started
Job `cron.daily' terminated
Conclusion
In this article, we have discussed cron and anacron and how to use them to schedule a job. We also did a comparison between them which will help you to choose the one based on your job requirements and system availability period.
Both Cron and Anacron allow you to run scheduled jobs. However, they work in very difference manners. We hope this helps you decide which job scheduling mechanism is right for you. See the resources section below for further reading.
Resources
- Crontab Man Page for further explanation on setting cron jobs
- Anacrontab Man Page
- More articles on cron
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
2 Comments
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (51)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)
How do you get Cron to run a php script instead?
It depends on exactly what you are trying to do. However, this two most common ways are to either just call the php executable followed by the script path like
php /path/to/php/file.php
or use curl like socurl -s https://example.com/myfile.php