issue70:tutoriel_cron
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue70:tutoriel_cron [2013/05/25 21:11] – [3] fredphil91 | issue70:tutoriel_cron [2013/05/26 14:24] (Version actuelle) – [12] auntiee | ||
---|---|---|---|
Ligne 8: | Ligne 8: | ||
Each user specifies their schedule via a “crontab” which can be listed or edited via the crontab command. We can list our crontab with the crontab -l command:** | Each user specifies their schedule via a “crontab” which can be listed or edited via the crontab command. We can list our crontab with the crontab -l command:** | ||
- | C'est très facile à mettre en place - même si, plus tard, je vais utiliser un exemple un peu compliqué pour illustrer l' | + | C'est très facile à mettre en place - même si, plus tard, je vais utiliser un exemple un peu compliqué pour en illustrer l' |
Si vous avez plusieurs utilisateurs configurés sur votre machine, chaque utilisateur a sa propre programmation indépendante qu'il peut configurer. | Si vous avez plusieurs utilisateurs configurés sur votre machine, chaque utilisateur a sa propre programmation indépendante qu'il peut configurer. | ||
- | Les utilisateurs expérimentés reconnaîtront qu'il s'agit d'une description de Cron, pré-installé sur pratiquement toutes les machines Linux. L' | + | Les utilisateurs expérimentés reconnaîtront qu'il s'agit d'une description de cron, pré-installé sur pratiquement toutes les machines Linux. L' |
Chaque utilisateur définit sa programmation via un « crontab » qui peut être affiché ou modifié via la commande crontab. Nous pouvons afficher notre crontab avec la commande crontab -l : | Chaque utilisateur définit sa programmation via un « crontab » qui peut être affiché ou modifié via la commande crontab. Nous pouvons afficher notre crontab avec la commande crontab -l : | ||
Ligne 25: | Ligne 25: | ||
There are 5 fields to specify the date – and the formats can be made quite complicated.** | There are 5 fields to specify the date – and the formats can be made quite complicated.** | ||
- | Vous trouverez toujours ces commentaires descriptifs dans votre crontab | + | Vous trouverez toujours ces commentaires descriptifs dans votre crontab et vous devez les conserver. Pour personnaliser votre crontab, vous devez ajouter une (ou plusieurs) lignes contenant des informations d' |
- | Cron est un démon qui tourne en tâche de fond, et qui exécutera vos commandes au moment approprié. | + | Cron est un démon qui tourne en tâche de fond et qui exécutera vos commandes au moment approprié. |
- | Remarque : vos commandes seront exécutées avec vos privilèges d' | + | Remarque : vos commandes seront exécutées avec vos privilèges d' |
- | Il y a 5 champs pour préciser la date - et les formats peuvent être rendus assez compliqués. | + | Il y a 5 champs pour préciser la date, et les formats peuvent être rendus assez compliqués. |
====== 3 ====== | ====== 3 ====== | ||
Ligne 44: | Ligne 44: | ||
Le séparateur entre chacun des cinq champs est toujours un ou plusieurs espaces (ou des tabulations). Chaque champ peut contenir des spécifications complexes telles que 1-5,10-15 (mais ce sont généralement soit un astérisque, | Le séparateur entre chacun des cinq champs est toujours un ou plusieurs espaces (ou des tabulations). Chaque champ peut contenir des spécifications complexes telles que 1-5,10-15 (mais ce sont généralement soit un astérisque, | ||
- | Chacun est généralement un certain nombre d' | + | Chacun est généralement un certain nombre d' |
Nous pouvons également utiliser des noms dans les champs où cela a un sens. | Nous pouvons également utiliser des noms dans les champs où cela a un sens. | ||
Ligne 51: | Ligne 51: | ||
====== 4 ====== | ====== 4 ====== | ||
- | 1st field | + | **1st field |
Minute of the hour (0 - 59) | Minute of the hour (0 - 59) | ||
Ligne 64: | Ligne 64: | ||
5th field | 5th field | ||
- | Day of the week (0 – 7; both 0 and 7 mean Sunday, or names: mon, tue, wed, etc). | + | Day of the week (0 – 7; both 0 and 7 mean Sunday, or names: mon, tue, wed, etc).** |
+ | |||
+ | 1er champ | ||
+ | Minute de l' | ||
+ | |||
+ | 2e champ | ||
+ | Heure de la journée (0 - 23) | ||
+ | |||
+ | 3e champ | ||
+ | Jour du mois (1 - 31) | ||
+ | |||
+ | 4e champ | ||
+ | Mois de l' | ||
+ | |||
+ | 5e champ | ||
+ | Jour de la semaine (0 - 7 ; 0 et 7 signifient tous les deux dimanche ; ou des noms: mon, tue, wed, etc.) | ||
====== 5 ====== | ====== 5 ====== | ||
- | Examples | + | **Examples |
First, a bad example – this would run only if 13th June is a Monday (next occurs in 2016)! | First, a bad example – this would run only if 13th June is a Monday (next occurs in 2016)! | ||
Ligne 77: | Ligne 92: | ||
0 1 13 * * some-command | 0 1 13 * * some-command | ||
- | This would run at 01:00 on the 13th of each month. | + | This would run at 01:00 on the 13th of each month.** |
+ | |||
+ | Exemples | ||
+ | |||
+ | Tout d' | ||
+ | |||
+ | 0 1 13 jun mon une-commande | ||
+ | |||
+ | Plus raisonnablement, | ||
+ | |||
+ | 0 1 13 * * une-commande | ||
+ | |||
+ | Cela s' | ||
====== 6 ====== | ====== 6 ====== | ||
- | Suppose you want to log problems with a very bad connection, using a user-written script named .whatip.sh which is a (hidden) file in the home directory, you might run the following: | + | **Suppose you want to log problems with a very bad connection, using a user-written script named .whatip.sh which is a (hidden) file in the home directory, you might run the following: |
*/10 * * * * [ -x .whatip.sh ] && bash .whatip.sh 2>/ | */10 * * * * [ -x .whatip.sh ] && bash .whatip.sh 2>/ | ||
Ligne 90: | Ligne 117: | ||
0 1 28-31 * * some-script | 0 1 28-31 * * some-script | ||
- | would be suitable – with the script making an early exit if it isn't actually the very last day of the month. | + | would be suitable – with the script making an early exit if it isn't actually the very last day of the month.** |
+ | |||
+ | Supposons que vous souhaitiez enregistrer dans les journaux (log) les problèmes avec une très mauvaise connexion, en utilisant un script écrit par l' | ||
+ | |||
+ | */10 * * * * [ -x .whatip.sh ] && bash .whatip.sh 2>/ | ||
+ | |||
+ | Cela s' | ||
+ | |||
+ | Malgré la richesse des possibilités pour spécifier les heures auxquelles une commande doit être exécutée, il y a certaines spécifications qui ne peuvent pas être facilement indiquées, comme « exécuter le dernier jour du mois ». Pour de tels cas, une programmation d' | ||
+ | |||
+ | 0 1 28-31 * * un-script | ||
+ | |||
+ | serait appropriée, | ||
====== 7 ====== | ====== 7 ====== | ||
- | Editing crontab: using the crontab -e command. | + | **Editing crontab: using the crontab -e command. |
This will almost certainly not use your normal editor, such as gedit, but a more basic one – such as nano. This is perfectly good for editing configuration files and works in a non-GUI environment. | This will almost certainly not use your normal editor, such as gedit, but a more basic one – such as nano. This is perfectly good for editing configuration files and works in a non-GUI environment. | ||
Ligne 101: | Ligne 140: | ||
The function keys are listed across the bottom – but you need only two of those listed. | The function keys are listed across the bottom – but you need only two of those listed. | ||
- | Scroll down to the last line and enter your changes to your crontab. | + | Scroll down to the last line and enter your changes to your crontab.** |
+ | |||
+ | Modification du crontab : en utilisant la commande crontab -e. | ||
+ | |||
+ | Ceci n' | ||
+ | |||
+ | Vous avez toujours des touches telles que Inser, Suppr, Retour arrière. La navigation (haut, bas, etc.) se fait via les touches fléchées ainsi que page précédente et page suivante. Cependant, vous n'avez pas de barres de défilement d' | ||
+ | |||
+ | Les touches de fonction sont listées en bas, mais vous n'avez besoin que de deux d' | ||
+ | |||
+ | Faites défiler jusqu' | ||
====== 8 ====== | ====== 8 ====== | ||
- | I understand that the very last line must end with a newline – this may not be necessary. | + | **I understand that the very last line must end with a newline – this may not be necessary. |
Check, then press CTRL+O to write out your changes. | Check, then press CTRL+O to write out your changes. | ||
Ligne 112: | Ligne 161: | ||
If you want to disable a crontab specification, | If you want to disable a crontab specification, | ||
- | It's quite hard to come up with a crontab line which does useful work, without turning it into a script – but I do have a useful example (the credit for this belongs to an unknown author). | + | It's quite hard to come up with a crontab line which does useful work, without turning it into a script – but I do have a useful example (the credit for this belongs to an unknown author).** |
+ | |||
+ | Je crois que la dernière ligne doit se terminer par un saut de ligne - cela peut ne pas être nécessaire. | ||
+ | |||
+ | Vérifiez, puis appuyez sur CTRL + O pour enregistrer vos modifications. | ||
+ | |||
+ | Enfin, appuyez sur CTRL + X pour quitter. | ||
+ | |||
+ | Si vous souhaitez désactiver une spécification de crontab, commentez-la en insérant un # en début de ligne. | ||
+ | |||
+ | Il est assez difficile de trouver une ligne de crontab qui fasse un travail utile, sans le transformer en un script, mais j'ai un bon exemple (le mérite en revient à un auteur inconnu). | ||
====== 9 ====== | ====== 9 ====== | ||
- | First the problem: If you move a fair number of files around, then Nautilus will create a small thumbnail file for it. If you move the file, you get another thumbnail, and if you look at your system directories you will generate many thousands of thumbnails. The problem is that Nautilus never deletes thumbnails. | + | **First the problem: If you move a fair number of files around, then Nautilus will create a small thumbnail file for it. If you move the file, you get another thumbnail, and if you look at your system directories you will generate many thousands of thumbnails. The problem is that Nautilus never deletes thumbnails. |
To check if you have a problem, enter the following command: | To check if you have a problem, enter the following command: | ||
Ligne 124: | Ligne 183: | ||
The solution: Every day I run a cron job to delete thumbnails which were last accessed more than 7 days ago. The command part in the crontab is: | The solution: Every day I run a cron job to delete thumbnails which were last accessed more than 7 days ago. The command part in the crontab is: | ||
+ | |||
+ | find ~/ | ||
+ | |||
+ | D' | ||
+ | |||
+ | Pour vérifier si vous avez un problème, entrez la commande suivante : | ||
+ | |||
+ | du -sbh .thumbnails | ||
+ | |||
+ | Je reçois une valeur de 20M soit 20 Mo (cela représente environ 1 000 vignettes). Vous pouvez voir une valeur beaucoup plus grande. | ||
+ | |||
+ | La solution : chaque jour, j' | ||
find ~/ | find ~/ | ||
====== 10 ====== | ====== 10 ====== | ||
- | It is critical that the command is entered exactly as shown including the trailing \; | + | **It is critical that the command is entered exactly as shown including the trailing \; |
Because this command includes the rm (remove file) command, you may like to test it first by running another closely associated (and harmless) command in a terminal window: | Because this command includes the rm (remove file) command, you may like to test it first by running another closely associated (and harmless) command in a terminal window: | ||
Ligne 140: | Ligne 211: | ||
45 19 * * * find ~/ | 45 19 * * * find ~/ | ||
- | Or, each evening, at 19:45 remove excess thumbnails. | + | Or, each evening, at 19:45 remove excess thumbnails.** |
+ | |||
+ | Il est essentiel que la commande soit entrée exactement comme indiqué, notamment le « \; » final. | ||
+ | |||
+ | Comme cette commande contient la commande rm (supprimer le fichier), vous pouvez avoir envie de la tester d' | ||
+ | |||
+ | find ~/ | ||
+ | |||
+ | Remarquez le remplacement de la commande rm par la commande ls. | ||
+ | |||
+ | Une fois que vous êtes satisfait de l' | ||
+ | |||
+ | 45 19 * * * find ~/ | ||
+ | |||
+ | soit : chaque soir à 19h45 supprimer les vignettes en trop. | ||
====== 11 ====== | ====== 11 ====== | ||
- | If you have multiple users, you will need to repeat for each one. | + | **If you have multiple users, you will need to repeat for each one. |
Your crontab is actually stored in a sub-directory of the /var directory – so a system upgrade where you choose to replace all your system files (even if you retain your /home directory) will cause your crontabs to be lost. However, it is important that you edit your crontab only via the crontab command, as this incorporates some important error checking. | Your crontab is actually stored in a sub-directory of the /var directory – so a system upgrade where you choose to replace all your system files (even if you retain your /home directory) will cause your crontabs to be lost. However, it is important that you edit your crontab only via the crontab command, as this incorporates some important error checking. | ||
Ligne 149: | Ligne 234: | ||
Excursionary note | Excursionary note | ||
- | If you have not performed an install which involved overwriting your /home directory in the last year or two, then the thumbnail removal entry may work slightly differently – but the difference is marginal and rather historical. | + | If you have not performed an install which involved overwriting your /home directory in the last year or two, then the thumbnail removal entry may work slightly differently – but the difference is marginal and rather historical.** |
+ | |||
+ | Si vous avez plusieurs utilisateurs, | ||
+ | |||
+ | Votre crontab est en fait stocké dans un sous-répertoire du répertoire /var - donc une mise à niveau du système où vous choisissez de remplacer tous vos fichiers système (même si vous conservez votre répertoire /home) entraînera la perte de votre crontab. Cependant, il est important de modifier votre crontab seulement via la commande crontab, car elle contient une vérification d' | ||
+ | |||
+ | Remarque en passant | ||
+ | |||
+ | Si vous n'avez pas effectué une installation qui écrase votre répertoire /home depuis un an ou deux, la suppression des vignettes peut fonctionner un peu différemment - mais la différence est marginale et plutôt historique. | ||
====== 12 ====== | ====== 12 ====== | ||
- | Originally, in addition to setting creation and modification timestamps on files, Linux always recorded file access timestamps (this is the -atime in our crontab entry). This can be inefficient, | + | **Originally, in addition to setting creation and modification timestamps on files, Linux always recorded file access timestamps (this is the -atime in our crontab entry). This can be inefficient, |
+ | |||
+ | Note that frequently accessed directories would always be looked at more often than every 7 days and so (under the old scheme), their thumbnails would never be deleted. Under the new scheme, atime is never updated, and so even these thumbnails would be deleted and very quickly recreated every 7 days. It's not a noticeable problem for thumbnails – although I understand one or two older applications find the atime change to be a problem.** | ||
+ | |||
+ | À l' | ||
- | Note that frequently accessed directories would always be looked at more often than every 7 days and so (under the old scheme), their thumbnails would never be deleted. Under the new scheme, atime is never updated, and so even these thumbnails would be deleted and very quickly recreated every 7 days. It's not a noticeable problem for thumbnails – although I understand one or two older applications | + | Notez que les répertoires fréquemment utilisés seront toujours consultés plus souvent que tous les 7 jours et ainsi (avec l' |
====== 13 ====== | ====== 13 ====== | ||
- | To check your setting enter: | + | **To check your setting enter: |
cat /etc/fstab | grep /home | cat /etc/fstab | grep /home | ||
Ligne 169: | Ligne 266: | ||
(My /home is mounted as ext3 – yours is probably ext4). | (My /home is mounted as ext3 – yours is probably ext4). | ||
- | I have manually changed /etc/fstab to mount everything as noatime – which means no access timestamps are updated, just like the modern default. If you don't see noatime listed, or you see atime in its place, you should consider modifying your fstab file to use the noatime option unless there is a special reason not to. | + | I have manually changed /etc/fstab to mount everything as noatime – which means no access timestamps are updated, just like the modern default. If you don't see noatime listed, or you see atime in its place, you should consider modifying your fstab file to use the noatime option unless there is a special reason not to.** |
+ | |||
+ | Pour vérifier vos paramètres, | ||
+ | |||
+ | cat /etc/fstab | grep /home | ||
+ | |||
+ | dans un écran de terminal ; ça m' | ||
+ | |||
+ | # /home was on /dev/sda7 during installation | ||
+ | |||
+ | UUID=0648d2d1-9a41-4257-8b79-dfc7bc227e82 /home ext3 defaults, | ||
+ | |||
+ | (mon /home est monté en ext3 - le vôtre est probablement ext4). | ||
+ | |||
+ | J'ai changé manuellement le fichier /etc/fstab pour monter tout en noatime, ce qui signifie pas d' |
issue70/tutoriel_cron.1369509104.txt.gz · Dernière modification : 2013/05/25 21:11 de fredphil91