Donnerstag, 10. November 2011

Run Jobs asynchronous with anacron

Hi Girls and Guys,

today i will tell you something about anacron.

With anacron you are able to install and maintain a Crontab which runs periodically but asynchronous. Now what does this mean?

Imagine you have a Job for a laptop which you want to execute on regular basis but because you don't know on which times your computer actually is turned on and therefore it might be difficult and even impossible to setup such jobs on fixed times. Anacron does not assume that the machine you are working on is running 24/7 like a productive server.

I will guide you through setting up a job for your anacron having a virusscan with avgscan but first let me tell you a little bit more about anacron: First of all anacron is not an other cron daemon. Rather think of it as an extension to the already installed Cron Daemon. If you first

installing ancron for your distribution anacron sets up a script in /etc/cron.hourly to startup /usr/sbin/anacron every hour reading and executing the commands you have specified in your /etc/anacrontab. In openSUSE you can install anacron using zypper. Simply type:

zypper in cronie-anacron

as root to install it.

Now lets take a look at the /etc/anacrontab:

++++++++

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
RANDOM_DELAY=45
START_HOURS_RANGE=18-23

#period in days delay in minutes job-identifier command
@daily 30 cron.avg_l1zard avgscan -W /home/l1zard/.wine -awbHpPcmj -r /var/log/avg_l1zard.report /home/l1zard/

@daily 30 cron.avg_tmp avgscan -awbHpPcmj -r /var/log/avg_tmp.report /tmp/
@daily 30 cron.avg_root avgscan -awbHpPcmj -r /var/log/avg_root.report /root/

++++++++

The variable SHELL tells the anacron program which Shell is going to be used to execute the commands. The PATH variable is like the environment variable PATH in bash which tells the shell where to look for executable files. With MAILTO you can specify a list of users which will receive a system informing these users about success or failure regarding these jobs. With RANDOM_DELAY you can specify the maximum range of the random delay which is added to the base delay setup in column 2. The START_HOURS_RANGE is the most interesting part of this configuration file. Here you can setup the lapse of time per day in which anacron should run. This is useful if you use commands which are using a lot of system resources and you want anacron to this jobs in a time where you can effort such loss of performence to other tasks.

Now lets have a look at the more interesting table. The first column you can specify the period in days on which you wish running the jobs. You may also use macros such as @daily @monthly or @weekly to run jobs on daily, monthly or weekly basis. In the second column you say anacron the base delay. Anacron waits this amount of time plus the RANDOM_DELAY before it starts any jobs. This way you can be sure that anacron will give you some time to do other jobs after you have logged in. The 3rd column is the name of the spooler file and the id of the job. You will find a file called /var/spool/anacron/job_identifier.The last column of the jobtable is the job including all options and parameters given to this command. However you must ensure that the program you want to run is in the PATH of your configuration file.

So lets test our anacrontab as a final step. Executing

anacron -T && echo "ok"

will test the for us whether the anacrontab we just installed is valid. And with

anacron -fs

we can test whether the commands we have are doing what we want them to do for us. The -s option is used to tell anacron to execute jobs in a row rather than all together. Remember you get a mail where you will be informed about success or failure of the jobs you want to have anacron executed.

That's it for today folks.