java.net member

Rechercher dans ce site

Anacron how to

>> 31 May 2017

Why anacron



Cron can be used to run a task on regular basis. It cannot anyway run passed task. Example i want a a task to be run at 10:20, but at this time the computer was off and now it's 11:00, this means the task will wait tell next time the condition is met again.

Anacron, on the other hand, will run an action after a specified amount of time beginning when the computer is on.

Adding Tasks

To add a job add it to the file /etc/anacrontab, as root

Note:
You must have proper permission for tasks to be run.

Cron and anacron job files

[root@localhost ~]# cat /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed


[root@localhost ~]# 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 2 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


Adding a task

I will add a custom script, that will run as root.
Note here that I also customize the anacrontab file to suit my needs


[root@localhost ~]# cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/bash #i want to use bash instead of sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/root #the /root folder contains my script
#MAILTO=root #I want to send output to a file. Uncomment to mail it
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=0 #random 0 to 45 minutes added to job's time. want 0
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22 #start of action in 24h

#period in days delay in minutes job-identifier command
1 2 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 I added my job to be launched 5 minutes after power on of the computer every day

#you can give it any name (here "ka"). The script or other command must be in PATH above

1 5 ka ka_update &>> /root/log"

Anacron log file

A file under the /var/spool/anacron directory, with the same name as the job. This file will contain a single line that indicates the last time the job was executed.

[root@localhost ~]# ls -ld /var/spool/anacron/*
-rw-------. 1 root root 9 May 30 19:02 /var/spool/anacron/ka

[root@localhost ~]# cat /var/spool/anacron/ka
20170530

[root@localhost ~]# date
Wed May 31 07:30:05 CEST 2017

Some very useful options

Taken simply from man anacron


Testing anacrontab

-T Anacrontab testing. Tests the /etc/anacrontab configuration file for validity. If there is an error in the file, it is shown on the standard output and Anacron returns the value of 1. Valid anacrontabs return the value of 0.

Force execution.

You can force execution

-f Forces execution of all jobs, ignoring any timestamps.
-n Runs jobs immediately and ignores the specified delays in the /etc/anacrontab file. This options implies -s.

-s Serializes execution of jobs. Anacron does not start a new job before the previous one finished.


-d Does not fork Anacron to the background. In this mode, Anacron will output informational messages to standard error, as well as to syslog. The output of any job is mailed by Anacron.

Custom files and folders

You can use some custom files instead of the standard files
-t some_anacrontab Uses the specified anacrontab, rather than the /etc/anacrontab default one.

-S spooldir Uses the specified spooldir to store timestamps in. This option is required for users who wish to run anacron themselves.

# anacron -S /mySpool -t /etc/myAnacron  

Example
[root@localhost ~]# cat ka_update

#!/bin/bash

#check first if there is an update available
dnf check-update &> /dev/null #suppress all output

#exit code 0 no update, 1 error and 100 there is updates
r=$? #this contains dnf check-update exit code

echo "Exit code = $r"

if [[ $r -eq 100 ]]
then

dnf -y update &> /dev/null && echo "Updated $(date)"

elif [[ $r -eq 0 ]]
then echo "No updates"
elif [[ $r -eq 1 ]]
then echo "Error dnf update command"
fi

Execute the job
# anacron -d -f -n ka #execute the job named "ka" immediately
Anacron started on 2017-05-31
Will run job `ka'
Jobs will be executed sequentially
Job `ka' started
Job `ka' terminated
Normal exit (1 job run)

How things really work

crond is a service that is normally enabled by default, but anacron is not a service.

cron executes hourly a script located under /etc/cron.hourly, called 0anacron

[root@localhost ~]# cd /etc/cron.hourly/
[root@localhost cron.hourly]# ls
0anacron

This script is very simple, and well documented

[root@localhost cron.hourly]# cat 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 [ `cat /sys/class/power_supply/AC/online 2>/dev/null`x = 0x ]; then
exit 0
fi
/usr/sbin/anacron -s#when the 2 above conditions are false /sbin/anacron runs

Hope, this was helpful. Please enjoy

0 comments:

Post a Comment

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP