Cron and Anacron, are daemons that can schedule execution of recurring tasks to a certain point in time defined by the exact time, day of the month, month, day of the week, and week.
Cron jobs can run as often as every minute. However, the utility assumes that the system is running continuously and if the system is not on at the time when a job is scheduled, the job is not executed.
On the other hand, Anacron remembers the scheduled jobs if the system is not running at the time when the job is scheduled. The job is then exectuted as soon as the system is up. However, Anacron can only run a job once a day.
Pas instalimit eshte e nevojshme qe te ngreme sherbimin crond
[root@localhost student]# service crond start
[root@localhost student]# service crond status
crond (pid 7067) is running...
[root@localhost student]#
Configuring Anacron Jobs (once a day schedule)
The main configuration file to schedule jobs is the /etc/anacrontab file, which can be only accessed by the root user. T he file contains the following:
[root@localhost student]# cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
The first three lines define the variables that configure the environment in which the anacron tasks run:
SHELL — shell environment used for running jobs (in the example, the Bash shell) PATH — paths to executable programs MAILTO — username of the user who receives the output of the anacron jobs by email If the MAILTO variable is not defined (MAILTO=), the email is not sent.
The next two variables modify the scheduled time for the defined jobs:
RANDOM_DELAY — maximum number of minutes that will be added to the delay in minutes variable which is specified for each job
The minimum delay value is set, by default, to 6 minutes.
If RANDOM_DELAY is, for example, set to 12, then between 6 and 12 minutes are added to the delay in minutes for each job in that particular anacrontab. RANDOM_DELAY can also be set to a value below 6, including 0.
When set to 0, no random delay is added. This proves to be useful when, for example, more computers that share one network connection need to download the same data every day.
START _HOURS_RANGE — interval, when scheduled jobs can be run, in hours In case the time interval is missed, for example due to a power failure, the scheduled jobs are not executed that day.
The remaining lines in the /etc/anacrontab file represent scheduled jobs and follow this format:
period in days
delay in minutes
job-identifier
command
period in days — frequency of job execution in days
The property value can be defined as an integer or a macro (@daily, @weekly, @monthly),
where
@daily denotes the same value as integer 1,
@weekly the same as 7, and
@monthly specifies that the job is run once a month regarless of the length of the month.
delay in minutes — number of minutes anacron waits before executing the job
The property value is defined as an integer.
If the value is set to 0, no delay applies.
job-identifier — unique name referring to a particular job used in the log files
command — command to be executed
The command can be either a command such as ls /proc >> /tmp/proc or a command which executes a custom script.
Any lines that begin with a hash sign (#) are comments and are not processed.
Examples of Anacron Jobs
The following example shows a simple /etc/anacrontab file:
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
#the maxim al random delay added to the base delay of the jobs
RANDOM_DELAY=30
# the jobs will be started during the following hours only
START_HOURS_RANGE=16-20
#period in days delay in m inutes job-identifier com m and
1 20 dailyjob nice run-parts /etc/cron.daily
7 25 weeklyjob /etc/weeklyjob.bash
@ monthly 45 monthlyjob ls /proc >> /tmp/proc
All jobs defined in this anacrontab file are randomly delayed by 6-30 minutes and can be executed between 16:00 and 20:00.
The first defined job is triggered daily between 16:26 and 16:50 (RANDOM_DELAY is between 6 and 30 minutes; the delay in minutes property adds 20 minutes).
The command specified for this job executes all present programs in the /etc/cron.daily directory using the run-parts script (the run-partsscripts accepts a directory as a command-line argument and sequentially executes every program in the directory).
me poshte po listojme permbajtjen e dosjes /etc/cron.daily
[root@localhost student]# ls /etc/cron.daily/ -l
total 24
-rwxr-xr-x. 1 root root 196 Aug 15 2012 logrotate
-rwxr-xr-x. 1 root root 905 Feb 21 2013 makewhatis.cron