10 Automatizimi i veprimeve

Cron Jobs

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.

Instalimi i CRON

[root@localhost student]# yum install cronie

Loaded plugins: fastestmirror, refresh-packagekit, security

Loading mirror speeds from cached hostfile




cronie.i686 0:1.4.4-12.el6

Dependency Updated:

cronie-anacron.i686 0:1.4.4-12.el6


[root@localhost student]#

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.




# the maximal random delay added to the base delay of the jobs


# the jobs will be started during the following hours only


#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

[root@localhost student]#

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


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 — frequency of job execution in days

The property value can be defined as an integer or a macro (@daily, @weekly, @monthly),


  • @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:




#the maxim al random delay added to the base delay of the jobs


# the jobs will be started during the following hours only


#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


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

-rwxr-xr-x. 1 root root 174 Sep 24 2012 mlocate.cron

-rwxr-xr-x. 1 root root 2126 Apr 23 2010 prelink

-rwxr-xr-x. 1 root root 563 Aug 23 2010 readahead.cron

-rwxr-xr-x. 1 root root 365 Oct 15 2009 tmpwatch

Configuring Cron Jobs

The configuration file for cron jobs is the /etc/crontab, which can be only modified by the root user.

The file contains the following:





# For details see m an 4 crontabs

# Exam ple of job definition:

# .---------------- m inute (0 - 59)

# | .------------- hour (0 - 23)

# | | .---------- day of m onth (1 - 31)

# | | | .------- m onth (1 - 12) OR jan,feb,m ar,apr ...

# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,m on,tue,wed,thu,fri,sat

# | | | | |

# * * * * * usernam e com m and to be executed

The first three lines contain the same variable definitions as an anacrontab file: SHELL, PATH, and MAILTO.

In addition, the file can define the HOME variable. The HOME variable defines the directory, which will be

used as the home directory when executing commands or scripts run by the job.

The remaining lines in the /etc/crontab file represent scheduled jobs and have the following format:

The following define the time when the job is to be run:

minute — any integer from 0 to 59

hour — any integer from 0 to 23

day — any integer from 1 to 31 (must be a valid day if a month is specified)

month — any integer from 1 to 12 (or the short name of the month such as jan or feb)

day of week — any integer from 0 to 7, where 0 or 7 represents Sunday (or the short name of the

week such as sun or mon)

The following define other job properties:

username — specifies the user under which the jobs are run

command — the 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.

For any of the above values, an asterisk (*) can be used to specify all valid values.

If you, for example, define the month value as an asterisk, the job will be executed every month within the constraints of the

other values.

A hyphen (-) between integers specifies a range of integers. For example, 1-4 means the integers 1, 2,

3, and 4.

A list of values separated by commas (,) specifies a list. For example, 3, 4 , 6, 8 indicates exactly

these four integers.

The forward slash (/) can be used to specify step values.

The value of an integer will be skipped within a range following the range with /integer.

For example, minute value defined as 0-59/2 denotes every other minute in the minute field.

Step values can also be used with an asterisk.

For instance, if the month value is defined as * /3, the task will run every third month.

Any lines that begin with a hash sign (#) are comments and are not processed.

Users other than root can configure cron tasks with the crontab utility.

The user-defined crontabs are stored in the /var/spool/cron/ directory and executed as if run by the users that created them.