Anacron in Linux, like cron, enables you to set periodic tasks on your computer. In this article, I will talk about what is anacronjob, the difference between anacron and cron, and how to create an anacron job.
Table of Contents
- Difference between anacron and cronjob
- Installation of anacron in linux
- How to create an anacron job in linux
- Conclusion
Difference between anacron and cronjob
Anacron is a little different from cron. Unlike cron, it does not assume that your system is running continuously. And that is useful in the following cases:
- Your laptop/desktop computer shuts down/hibernates/sleeps while a job’s execution was underway.
- The computer is off while a job was supposed to get executed.
The next time, the computer starts the cronjob is executed. For example, Let’s say you have scheduled your computer backup every 7th day. Each day when your computer starts, anacron checks if the backup command has been executed once in the last 7 days. If it is not, it will execute the command on that day.
Installation of anacron in Linux
Most of the time, it is installed with cron. So, check your computer if anacron is installed. You can use which
or locate
command for this.
$ which anacron
/usr/bin/anacron
$ locate anacron
/etc/anacrontab
/etc/cron.hourly/0anacron
/usr/bin/anacron
/usr/share/man/man5/anacrontab.5.gz
/usr/share/man/man8/anacron.8.gz
How to create an anacron job in linux
To create an anacron job, you need to use a file /etc/anacrontab
.
Do not delete any line from it. Just add your job in the end. It might look like the following in your computer:
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
RANDOM_DELAY=45
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
1 0 pacman_database pacman -Sy
3 0 backup /home/ajay/.my_scripts/rsync.sh -b
7 0 anki_sync /home/ajay/.my_scripts/anki_sync.sh
Here,
- SHELL, PATH, and MAILTO define the shell, the path for your scripts, and email. Anacron uses the mail to send the output in case something goes wrong. Don’t worry if you have not set up email on your computer. Just leave the
MAILTO
line as it is. Anacron will only throw the errorCan't find sendmail at /usr/sbin/sendmail, not mailing output
in its logs and everything else will work. - START_HOURS_RANGE defines the hours in which your jobs will be started. Here, it is 3 AM to 10 PM. If your computer is shut down/sleeps during these hours, your jobs will not be executed. You can even put in a more rigorous time
0-24
in case, your computer is turned off or in suspension mode very often. - RANDOM_DELAY: Anacron adds this (for the above example, anything in between 0 and 45) to the delay in minutes you define for each job. Both units are in minutes. Anacron waits for the total delay before the job is executed.
Explanation of columns in /etc/anacrontab
The first column defines periods in days. 1 means every 1st day. 3 means every 3rd day and so on. It can also be @monthly
, @daily
, @weekly
for monthly, daily, and weekly execution of jobs.
The second column defines the delay in minutes. Anacron adds this to RANDOM_DELAY and waits for the total delay before the job is executed.
The third column is the job identifier. It is just a name so it can be anything. Anacron uses it in logs such as journlctl
. Use it to see if your anacron is executing your jobs or not. For example, to check if anacron is executing the rsync backup or not, run the command journalctl --unit=cronie.service
(other linux distros might use a different word instead of cronie
) and search for your job identifier backup
in the output (use /
or ?
or grep
):
$ journalctl --unit=cronie.service | grep backup
Mar 02 12:01:01 legion anacron[34119]: Will run job `backup' in 19 min.
Mar 02 12:20:24 legion anacron[34119]: Job `backup' started.
Mar 02 12:21:20 legion anacron[34119]: Job `backup' terminated.
Mar 05 19:01:01 legion anacron[148378]: Will run job `backup' in 4 min.
Mar 05 19:05:17 legion anacron[148378]: Job `backup' started.
Mar 05 19:05:30 legion anacron[148378]: Job `backup' terminated.
Mar 08 00:01:01 legion anacron[263525]: Will run job `backup' in 29 min.
Mar 08 00:31:14 legion anacron[263525]: Job `backup' started
...
The fourth column is a command. Write the full path in case it is not included in the PATH variable defined in /etc/anacrontab
.
Conclusion
That’s all. For more, see anacron’s manuals – man 8 anacron
and man 5 anacrontab
. If you have any recommendations/questions, tell them in the comment section below. Thanks.
Pingback: How to send notifications in linux using dunstify/notify-send | SmartTech101