Digit Oktavianto Web Log

Catatan Sampah si Digit

Cron and Anacron Scheduler

| Comments

I got information from The Geek Stuff Blog about Cron Vs Anacron : http://www.thegeekstuff.com/2011/05/anacron-examples/. I want to rewrite the article, and add some information here.

Cron is great, but it has one weakness: it is designed for machines that are left running continuously. If your machine is turned off when a job falls due, it simply won’t be run, Anacron is a periodic command scheduler similar to some uses of cron, but it does not assume that the system is running continuously. Anacron useful to catch missed daily cron job when your computer is not in active condition.

I have some application that scheduled in cronjob. But i have a problem, where my box is not always in active condition (off). When my computer in inactive condition, my application is not executed by cronjob. This is problem for me, until i find anacron. Anacron will executed scheduler job in active condition. If your computer stay in inactive (off) condition, the job will not executed. Anacron will execute your job once after your computer up or active.

Anacron configuration file located in /etc/anacrontab. Format of anacron :

1
period   delay   job-identifier   command

Field 1 is Recurrence period: This is a numeric value that specifies the number of days.

1 – daily

7 – weekly

30 – monthly

N – This can be any numeric value. N indicates number of days Note: You can also use ‘@monthly’ for a job that needs to be executed monthly.

Field 2 is Delay: This indicates the delay in minutes. i.e X number of minutes anacron should wait before executing the job after the the machine starts.

Field 3 is Job identifier: It is the name for the job’s timestamp file. It should be unique for each job. This will be available as a file under the /var/spool/anacron directory. This file will contain a single line that indicates the last time when this job was executed.

# ls -1 /var/spool/anacron/

test.daily

cron.daily

cron.monthly

cron.weekly

# cat /var/spool/anacron/test.daily 20110507

Field 4 is command: Command or shell script that needs to be executed.

Just like shell scripts, comments inside anacrontab file starts with #

Example of use Anacron :

digit@digit-laptop:~$  sudo cat /etc/anacrontab
1       15      cron.daily     /home/digit/dailyreport.sh

The example job schedule named dailyreport.sh will executed everyday (1) 15 minutes after the pc or laptop active / on. But what happens if the laptop or pc not in off condition? When the job scheduler executed?

Based on this article, Anacron have two methods to start. Two methods of running Anacron on a regular basis are common: You can launch Anacron whenever the computer starts, or you can launch the program via a Cron job. Anacron handles system tasks, but you can also use it to handle the equivalent of user Cron jobs too, so you may want to configure it to run both types of jobs.

Anacron run in startup by default. If you want to run Anacron via cron jobs, you should do some changes. If the computer is shut down and restarted less frequently than once a day, or if you use sleep mode rather than a full shutdown, you may want to run Anacron via a Cron job. You can create an entry in your /etc/crontab file, such as the following, to do the job:

1
17 * * * * root anacron

This entry runs Anacron on the 17th minute of every hour, at which time it checks for jobs and, if necessary, runs them. Note that Anacron runs its jobs only as necessary, so running it less frequently than once an hour gains you little, unless you can predict when the system will be up—and in that case, you might as well use Cron to schedule your jobs.

Running Cron and Anacron Together

I find same case with my case from Serverfault.

I am wondering what will happen if my computer is shutdown during the time or it was turned off before the time when it was schedule to do something ?

Yes, your job won’t be executed by Cron job when your computer in off / inactive condition. Your cron job will execute next loop, for example you make weekly cron job, the job won’t be executed until next week.

If i want to use cron, but when my systme suddenly in off / inactive condition? What should i do? Cron and Anacron is the answer. This is step bys etep example :

As another example, assuming you have a script here: /usr/local/sbin/maint.sh

And you wish to run it every three days, the standard entry in /etc/crontab would look like this:

# m h dom mon dow user  command
0 0 */3 * * root /usr/local/sbin/maint.sh

If your computer was not on at 00:00 on the 3rd of the month, the job would not run until the 6th.

To have the job instead run on the 4th when the computer is off and “misses” the run on the 3rd, you’d use this in /etc/anacrontab (don’t forget to remove the line from /etc/crontab):

# period delay job-identifier command
3 5 maint-job /usr/local/sbin/maint.sh

The “delay” of “5” above means that anacron will wait for 5 minutes before it runs this job. The idea is to prevent anacron from firing things off immediately at boot time.

(Example taken from here.

I also have another example to run Cron and Anacron together From Tuxradar Tutorial : http://www.tuxradar.com/content/automate-linux-cron-and-anacron. But i haven’t test it in my box.


Good Luck :D


Source :

http://www.thegeekstuff.com/2011/05/anacron-examples/

http://www.ibm.com/developerworks/linux/library/l-anacron/index.html

http://serverfault.com/questions/52335/job-scheduling-using-crontab-what-will-happen-when-computer-is-shutdown-during

http://www.tuxradar.com/content/automate-linux-cron-and-anacron

Comments