LINUX-14
JOB SCHEDULING (ANACRON)
We studied about cron in previous post; it is enough to take care of
my scheduled jobs.
Then Why anacron…??
Let’s say your company has not paid the vendor for AMC renewal. The
UPS of rack is faulty. And the state electricity board has some problem which
continues to next 10 days.
Now What…?
Practically this example is next to impossible, b’coz improper &
random power outage for servers without backup power can lead to severe
Hardware damage to servers. So, no company will take this risk.
Anyways… this is just example.
Now consider that there are several imp scripts are scheduled in
crontab for various departments.
Can we assure them that there data will be fetched correctly at same
time when they scheduled…?
Might be Yes… Might be No, b’coz I don’t know the status of power at
time of scheduled jobs.
Then what…??
We have to compromise the data of that minute/ hour/ day… right??
Isn’t it would be great if I have a system which ensure my data,
irrespective of power supply. Means it
doesn’t matter power is there or not at the time of my scheduled jobs.
In this scenario my intention is to at least get the daily data, not
to get it on time.
This can be done via “anacron”.
- · Anacron is only set by root user.
- · Minimum entity with Cron is minute while it is in days with Anacron.
- · Anacron runs only on daily basis, does not entertain minute & hour field.
- · Cron expects the system to be up and running while the Anacron doesn’t expect the system to be up and running all the time.
- · Cron should be used when we want a job to be executed at a particular hour and minute while Anacron should be used in when the job can be executed irrespective of the hour and minute.
- · Default file for anacron is /etc/anacrontab.
- · Anacron controls @daily, @weekly, @monthly jobs.
- · Anacron is not a replacement of cron.
PACKAGE…………………..
[root@rhel7-server ~]# rpm -qa cronie-anacron
cronie-anacron-1.4.11-11.el7.x86_64
[root@rhel7-server ~]# rpm -qa cronie
cronie-1.4.11-11.el7.x86_64
[root@rhel7-server ~]# 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
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
FROM RH MAN PAGES………………..
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 — user name 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 regardless 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
later above one’s are explained with example…………
Anacron has no DAEMON associated with it; it runs at the time of booting
and at this time, Anacron reads the /etc/anacrontab file then, check the
time stamp of files in /var/spool/anacron to see when each of the jobs
was last run, and figures out what needs to get run.
RUN-PARTS:
Run-parts is a simple utility that runs all the executable scripts in
the given directory.
It is used extensively with cron because it allows us to run
multiple scripts at a specified time by placing them in a single location.
Anacron is managed by CRON….
HOW…??
List /etc/cron.d,
[root@rhel7-server /]# ls -l /etc/cron.d
total 24
-rw-r--r--. 1 root root 128 Jan 27
2014 0hourly
-rw-r--r--. 1 root root 108 Mar 10
2014 raid-check
-rw-r--r--. 1 root root 73
Jan 8 11:16 sc1.sh
-rw-r--r--. 1 root root 72
Jan 8 11:33 sc2
-rw-r--r--. 1 root root 235 Jan 27
2014 sysstat
-rw-r--r--. 1 root root 187 Jan 27
2014 unbound-anchor
Found an entry 0hourly in /etc/cron.d
[root@rhel7-server /]# cat /etc/cron.d/0hourly
# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 * * * * root run-parts /etc/cron.hourly
By default, the crond daemon starts anacron through the /etc/cron.hourly/0anacron
cron job. This execution only occurs once
per day (verification is done via the /var/spool/anacron/cron.daily
file) and only if the machine doesn’t run on battery power.
[root@rhel7-server /]# ls -l /etc/cron.hourly
total 8
-rwxr-xr-x. 1 root root 392 Jan 27
2014 0anacron
-rwxr-xr-x. 1 root root 362 Apr
8 2014 0yum-hourly.cron
Found an entry for 0anacron
[root@rhel7-server /]# cat /etc/cron.hourly/0anacron
#!/bin/sh
# Check whether 0anacron was run today already
if test -r /var/spool/anacron/cron.daily; then
day=`cat
/var/spool/anacron/cron.daily`
fi
if [ `date +%Y%m%d` = "$day" ]; then
exit 0;
fi
# Do not run jobs when on battery power
if test -x /usr/bin/on_ac_power; then
/usr/bin/on_ac_power
>/dev/null 2>&1
if test $? -eq 1; then
exit 0
fi
fi
/usr/sbin/anacron –s
cron.daily directory includes a script called 0anacron that runs anacron,
but with the -u option. So, we see that Anacron is run by Cron every day.
However, the -u option tells Anacron merely to update its timestamps
and not to actually run the commands.
This script (0anacron) reads the contents of /var/spool/anacron/cron.daily
to check, whether the “anacron” has been run in current date or not. If not and
if the system is not on battery power then the “/usr/sbin/anacron –s” command
is executed which runs scripts defined in the /etc/anacrontab file.
[root@rhel7-server /]# ls -l /var/spool/anacron/cron.daily
-rw-------. 1 root root 9 Jan
8 10:36 /var/spool/anacron/cron.daily
[root@rhel7-server /]# cat /var/spool/anacron/cron.daily
20170108
[root@rhel7-server /]# ls -l /var/spool/anacron/
total 12
-rw-------. 1 root root 9 Jan
8 10:36 cron.daily
-rw-------. 1 root root 9 Dec 26 17:15 cron.monthly
-rw-------. 1 root root 9 Jan
7 15:09 cron.weekly
How to set a anacron job………..
A B C D
#period in
days delay in minutes job-identifier command
2 0 my-ping-status /myscript1.sh
FIELD “A”…………
Period in days
denotes occurrence of job
@daily 1
@weekly 7
@monthly 28/30/31 “independent
on the length of the month”
FIELD “B”…………
Delay in minutes means N
no of minutes anacron needs to wait after booting of machine to execute the
job.
To prevent all the tasks running at same time.
0 means, run as system is up.
FIELD “C”…………
Job-identifier is unique
name for the job in log files. Description of the task, used in Anacron
messages and as the name of the job's timestamp file, can contain any non-blank
characters (except slashes).
FIELD “D”…………
Command is the
command to execute
Consider “Random Delay” and “delay in minutes” value……………………..
Random Delay is maximum delay; the minimum delay value is by default 6
minutes.
So if the “random delay” is 30 and “delay in minutes” value is 20,
then when the job will run…??
Add the min delay 6M to “delay in minutes” which will be
6+20= 26
Add the max delay 30M to “delay in minutes” which will be
30+20= 50
The job will run in between 26th to 50th minute.
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
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
Here the daily job will run in between 03:00am to 10:00pm (as
defined in START_HOURS_RANGE) and
random delay jobs by between 11 and 50 minutes.
The job scripts in /etc/cron.daily, run anywhere between 03:11 and
03:50 every day if the system is running,
Or if the system is booted and the time is less than 22:00. The
run-parts script sequentially executes every program within the directory
specified as its argument.
Scripts in /etc/cron.weekly run once per week with a delay offset of
between 31 and 70 minutes.
Scripts in /etc/cron.monthly run once per month with a delay offset
of between 51 and 90 minutes.
Now let’s set the anacron job…………………………..
[root@rhel7-server Desktop]# 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=10 ç I Changed Here
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days delay in
minutes job-identifier command
1 0 home-dir-status /script.sh ç 1st Job
1 0 ping-status2 /myscript1.sh ç 2nd job
Consider to have space only.
[root@rhel7-server /]# more script.sh
#!/bin/sh
ls -ltr /home >> /test.txt
date >> /test.txt
[root@rhel7-server /]# >test.txt
[root@rhel7-server /]# cat test.txt
[root@rhel7-server /]# ./script.sh
[root@rhel7-server /]# cat test.txt
total 56
drwx------. 14 araman araman
4096 Dec 30 11:17 araman
-rw-r--r--. 1 root root
30720 Jan 1 16:57 user1.bkp.tar
drwx------. 5 user3 user3
4096 Jan 4 18:27 user3
drwx------. 5 user4 user4
4096 Jan 4 18:58 user4
drwx------. 5 user5 user5
4096 Jan 4 19:14 user5
drwx------. 8 user1 user1
4096 Jan 4 19:31 user1
drwx------. 8 raman raman
4096 Jan 5 12:10 raman
Mon Jan 9 16:54:18 IST 2017 ç check the timing
[root@rhel7-server /]# ./myscript1.sh
PING 192.168.234.142 (192.168.234.142) 56(84) bytes of data.
64 bytes from 192.168.234.142: icmp_seq=1 ttl=64 time=0.063 ms
64 bytes from 192.168.234.142: icmp_seq=2 ttl=64 time=0.103 ms
--- 192.168.234.142 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.063/0.083/0.103/0.020 ms
how are you ?
Running anacron manually………………..
Currently I am at /var/spool/anacron dir,
[root@rhel7-server anacron]# ls -ltr
total 12
-rw-------. 1 root root 9 Jan
9 16:46 cron.weekly
-rw-------. 1 root root 9 Jan
9 16:46 cron.monthly
-rw-------. 1 root root 9 Jan
9 16:52 cron.daily
[root@rhel7-server anacron]# updatedb
[root@rhel7-server anacron]# anacron –u
Check the effect of above command,
[root@rhel7-server anacron]# ls -ltr
total 20
-rw-------. 1 root root 9 Jan
9 17:04 home-dir-status ç job-identifier created
-rw-------. 1 root root 9 Jan
9 17:04 ping-status2 ç job-identifier created
-rw-------. 1 root root 9 Jan
9 17:04 cron.weekly
-rw-------. 1 root root 9 Jan
9 17:04 cron.monthly
-rw-------. 1 root root 9 Jan
9 17:04 cron.daily
Job-identifiers for jobs are created as well the time of all files
are also updated.
Let’s check the scripts ran or not…??
[root@rhel7-server anacron]# cat /test.txt
total 56
drwx------. 14 araman araman
4096 Dec 30 11:17 araman
-rw-r--r--. 1 root root
30720 Jan 1 16:57 user1.bkp.tar
drwx------. 5 user3 user3
4096 Jan 4 18:27 user3
drwx------. 5 user4 user4
4096 Jan 4 18:58 user4
drwx------. 5 user5 user5
4096 Jan 4 19:14 user5
drwx------. 8 user1 user1
4096 Jan 4 19:31 user1
drwx------. 8 raman raman
4096 Jan 5 12:10 raman
Mon Jan 9 16:54:18 IST 2017
NO……..
[root@rhel7-server anacron]# anacron -n
[root@rhel7-server anacron]# anacron –f
Meaning of arguments,
-f : Forces execution
of all jobs, ignoring any timestamps.
-u : updates the
timestamps of all jobs to the current date, but don’t run any
-n : Runs jobs
immediately and ignores the specified delays in the
/etc/anacrontab file.
This option implies -s.
-s : Serializes execution
of jobs. Anacron does not start a new job before the previous one finished.
Let’s check the job output.
[root@rhel7-server anacron]# cat /test.txt
total 56
drwx------. 14 araman araman
4096 Dec 30 11:17 araman
-rw-r--r--. 1 root root
30720 Jan 1 16:57 user1.bkp.tar
drwx------. 5 user3 user3
4096 Jan 4 18:27 user3
drwx------. 5 user4 user4
4096 Jan 4 18:58 user4
drwx------. 5 user5 user5
4096 Jan 4 19:14 user5
drwx------. 8 user1 user1
4096 Jan 4 19:31 user1
drwx------. 8 raman raman
4096 Jan 5 12:10 raman
Mon Jan 9 16:54:18 IST 2017
total 56
drwx------. 14 araman araman
4096 Dec 30 11:17 araman
-rw-r--r--. 1 root root
30720 Jan 1 16:57 user1.bkp.tar
drwx------. 5 user3 user3
4096 Jan 4 18:27 user3
drwx------. 5 user4 user4
4096 Jan 4 18:58 user4
drwx------. 5 user5 user5
4096 Jan 4 19:14 user5
drwx------. 8 user1 user1
4096 Jan 4 19:31 user1
drwx------. 8 raman raman
4096 Jan 5 12:10 raman
Mon Jan 9 17:04:52 IST 2017 çcheck the timing with job-identifiers
You have new mail in /var/spool/mail/root
O/P from /var/spool/mail/root
Date: Mon, 9 Jan 2017 17:04:53
+0530 (IST) çcheck the timing
PING 192.168.234.142 (192.168.234.142) 56(84) bytes of data.
64 bytes from 192.168.234.142: icmp_seq=1 ttl=64 time=0.068 ms
64 bytes from 192.168.234.142: icmp_seq=2 ttl=64 time=0.076 ms
--- 192.168.234.142 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.068/0.072/0.076/0.004 ms
how are you ?
GREAT……………………………..
Now check the status of /var/log/cron while running “anacron” command
with various options,
Jan 9 17:04:11 rhel7-server anacron[3221]:
Updated timestamp for job `home-dir-status' to 2017-01-09
Jan 9 17:04:11 rhel7-server anacron[3221]:
Updated timestamp for job `ping-status2' to 2017-01-09
Jan 9 17:04:11 rhel7-server anacron[3221]:
Updated timestamp for job `cron.daily' to 2017-01-09
Jan 9 17:04:11 rhel7-server anacron[3221]:
Updated timestamp for job `cron.weekly' to 2017-01-09
Jan 9 17:04:11 rhel7-server anacron[3221]:
Updated timestamp for job `cron.monthly' to 2017-01-09
Jan 9 17:04:44 rhel7-server anacron[3234]:
Anacron started on 2017-01-09
Jan 9 17:04:44 rhel7-server anacron[3234]: Normal
exit (0 jobs run)
Jan 9 17:04:52 rhel7-server anacron[3236]: Anacron
started on 2017-01-09
Jan 9 17:04:52 rhel7-server anacron[3236]: Job
`cron.weekly' locked by another anacron - skipping
Jan 9 17:04:52 rhel7-server anacron[3236]: Job
`cron.monthly' locked by another anacron - skipping
Jan 9 17:04:52 rhel7-server anacron[3236]: Will
run job `home-dir-status' in 0 min.
Jan 9 17:04:52 rhel7-server anacron[3236]: Will
run job `ping-status2' in 0 min.
Jan 9 17:04:52 rhel7-server anacron[3236]: Will
run job `cron.daily' in 5 min.
Jan 9 17:04:52 rhel7-server anacron[3236]: Job
`home-dir-status' started
Jan 9 17:04:52 rhel7-server anacron[3236]: Job
`ping-status2' started
Jan 9 17:04:52 rhel7-server anacron[3236]: Job
`home-dir-status' terminated
Jan 9 17:04:53 rhel7-server anacron[3236]: Job
`ping-status2' terminated (mailing output)
^C
To run “anacron” manually only “anacron –f” is sufficient,
[root@rhel7-server /]# anacron -f
[root@rhel7-server /]# ls -ltr /var/spool/anacron/
total 20
-rw-------. 1 root root 9 Jan
9 17:32 cron.monthly
-rw-------. 1 root root 9 Jan
9 17:36 cron.daily
-rw-------. 1 root root 9 Jan
9 17:55 cron.weekly
-rw-------. 1 root root 9 Jan
9 18:19 home-dir-status çcheck the timing
-rw-------. 1 root root 9 Jan
9 18:19 ping-status2 çcheck the timing
[root@rhel7-server /]# cat test.txt
total 56
drwx------. 14 araman araman
4096 Dec 30 11:17 araman
-rw-r--r--. 1 root root
30720 Jan 1 16:57 user1.bkp.tar
drwx------. 5 user3 user3
4096 Jan 4 18:27 user3
drwx------. 5 user4 user4
4096 Jan 4 18:58 user4
drwx------. 5 user5 user5
4096 Jan 4 19:14 user5
drwx------. 8 user1 user1
4096 Jan 4 19:31 user1
drwx------. 8 raman raman
4096 Jan 5 12:10 raman
Mon Jan 9 18:19:06 IST 2017 çcheck the timing
You have new mail in /var/spool/mail/root
[root@rhel7-server /]# tail -14 /var/spool/mail/root
Content-Type: text/plain; charset="UTF-8"
Subject: Anacron job 'ping-status2' on rhel7-server çcheck the msg
Message-Id:
<20170109124907.E1D2C2333F8E@rhel7-server.localdomain>
Date: Mon, 9 Jan 2017 18:19:07
+0530 (IST) çcheck the timing
PING 192.168.234.142 (192.168.234.142) 56(84) bytes of data.
64 bytes from 192.168.234.142: icmp_seq=1 ttl=64 time=0.064 ms
64 bytes from 192.168.234.142: icmp_seq=2 ttl=64 time=0.084 ms
--- 192.168.234.142 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.064/0.074/0.084/0.010 ms
how are you ?
Status of /var/log/cron
Jan 9 18:19:06 rhel7-server anacron[4826]:
Anacron started on 2017-01-09
Jan 9 18:19:06 rhel7-server anacron[4826]: Will
run job `home-dir-status' in 0 min.
Jan 9 18:19:06 rhel7-server anacron[4826]: Will
run job `ping-status2' in 0 min.
Jan 9 18:19:06 rhel7-server anacron[4826]: Will
run job `cron.daily' in 5 min.
Jan 9 18:19:06 rhel7-server anacron[4826]: Will
run job `cron.weekly' in 25 min.
Jan 9 18:19:06 rhel7-server anacron[4826]: Will
run job `cron.monthly' in 45 min.
Jan 9 18:19:06 rhel7-server anacron[4826]: Job
`home-dir-status' started
Jan 9 18:19:06 rhel7-server anacron[4826]: Job
`ping-status2' started
Jan 9 18:19:06 rhel7-server anacron[4826]: Job
`home-dir-status' terminated
Jan 9 18:19:07 rhel7-server anacron[4826]: Job
`ping-status2' terminated (mailing output)
Anacron can be controlled by cronjobs also,
[root@rhel7-server /]# crontab -e
* 15 * * * root anacron
Or
[root@rhel7-server /]# crontab -e
30 * * * * root anacron
Or
Depends upon the necessity.
No comments:
Post a Comment