Outils pour utilisateurs

Outils du site


linux:ubuntu:cron

CRON

Vérifier si le service CRON tourne

sudo systemctl status cron

S'il faut l'installer :

sudo apt install cron

Configuration de CRON

Éditer le fichier /etc/crontab

sudo vi /etc/crontab
# 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
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

Dans /etc/crontab, le service est déjà configuré pour lancer les scripts se trouvant dans les sous-dossiers :

chaque heure à *:17 /etc/cron.hourly
chaque jour à 6:25 /etc/cron.daily
chaque semaine le dimanche à 6:47 /etc/cron.weekly
chaque mois le 1er à 6:52 /etc/cron.monthly

Ajouter une tâche

Ajouter une ligne au fichier /etc/crontab.

Par exemple :

55 * * * * root echo `date` >> /tmp/bonjour.txt

Pas besoin de relancer le service, la modification est prise en compte dès l'édition du fichier.

Exemples

Pour lancer une tâche toutes les 15 minutes :

*/15 * * * * <user> <commande>

Pour lancer une tâche chaque jour à 0h00, 6h00, 12h00 et 18h00 :

0 0,6,12,18 * * * <user> <commande>
linux/ubuntu/cron.txt · Dernière modification: 2021/03/16 20:20 par marclebrun