java.net member

Rechercher dans ce site

Anacron une action au démarrage

>> 31 May 2017


Pourquoi anacron

Si vous voulez lancer une action sur des périodes, tous les x temps, cron est votre ami.
par contre cron n'est pas capable de revenir en arrière. Si vous programmez une action à 3h du matin et vous allumez l'ordinateur à 9h, l'action ne s’exécute pas. Pour ce genre de missions il existe un programme nommé anacron, son job est de lancer une action après une période de temps, spécifié et compter à partir du démarrage de l'ordinateur.

Ajouter des action

Editez le fichier /etc/anacrontab et ajoutez des actions désirées, comme root

Note:
Il est nécessaire d'avoir les permissions nécessaires pour exécuter chaque action mentionnée.

Fichiers de cron et anacron

[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

Ajouter une action

J'ai mis quelques commandes dans un script. Le script est exécuté comme root
Note :
J'ai modifié quelques variables dans /etc/anacrontab


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

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

SHELL=/bin/bash #bash est utilisé au lieu de " sh "
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/root #J'ai ajouté /root, car contenant le script
#MAILTO=root #La sortie est dirigé vers un fichier. pour le mail dé-commentez
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=0 #0 à 45 minutes est ajoutés au temps
# 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


#Mon action commence après 5 minutes du démarrage de l'ordinateur, tous les jours
#Le nom est au choix (ici "ka"). Le programme doit être dans PATH
1 5 ka ka_update &>> /root/log"

Anacron fichier log

Anacron ajoute un fichier (même nom que l'action) sous /var/spool/anacron, ce fichier contient la dernière date de l'exécution de l'action. Cette même date est aussi dans /var/spool/anacron/cron.daily

[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

options

Les options sont dans man anacron

Tester anacrontab

-T Teste la validité de /etc/anacrontab.

Forcer l'exécution.



-f force toutes les actions indépendamment de leurs temps
-n force toutes les actions immédiatement (avec l'option s)


-s une action est exécutée après la fin d'une autre.


-d exécute directement (pas d'arrière plan)

Fichier et dossier personnalisés


-t fichier permet d'utiliser un autre fichier, que /etc/anacrontab

-S dossier utilise le dossier donné pour stocker la date..

# anacron -S /dossier-t /etc/fichierAnacron  

Example
[root@localhost ~]# cat ka_update

#!/bin/bash

#Recherche des updates
dnf check-update &> /dev/null #pas d'affichage sur la stdout ou stderr

#exit code 0 pas d'update, 1 erreur.100 des updates disponibles
r=$? #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 "Pas d'updates"
elif [[ $r -eq 1 ]]
then echo "Erreur dans la commande dnf check-update"
fi

Executer the job
# anacron -d -f -n ka #exécuter l'action "ka" immédiatement
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)

Cron et anacron ensemble

crond est un service lancé au démarrage, par contre anacron est un programme, ce n'est pas un service. cron lance à toutes les heures un script " 0anacron " situé sous /etc/cron.hourly

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


0anacron

Le script est simple, il vérifies deux choses, si la date actuelle est dans /var/spool/anacron.daily, le script est déjà exécuté donc pas besoin de le ré-exécuter. 2ème condition, si on travaille sur un ordinateur portable en utilisant la batterie, pas la peine d'exécuter l'action.

Si les 2 conditions échouent on lance anacron pour exécuter notre action.

[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#si les 2 conditions sont faux lance anacron

Vous avez trouvé cet article utile ! enjoy 



0 comments:

Post a Comment

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

Back to TOP