Wiki Ubuntu-it

Indice
Partecipa
FAQ
Wiki Blog
------------------
Ubuntu-it.org
Forum
Chiedi
Chat
Cerca
Planet
  • Pagina non alterabile
  • Informazioni
  • Allegati

Versione 1 del 20/11/2005 12.49.57

Nascondi questo messaggio

Cos'è Cron?

Cron è un demone usato per programmare azioni che verranno così eseguite in un tempo scelto. Ogni utente ha il suo file per crontab, permettendo loro di specificare le azioni da eseguire e il numero di volte che dovranno essere eseguite. C'è anche un crontab di sistema, permettendo compiti come azioni sui log, aggiornamento dei database che devono essere fatti regolarmente, etc...

Usare Cron

Per usare cron, semplicemente aggiungi delle voci al tuo file di crontab. Una voce di crontab è una stringa che specifica quando lanciare un comando e il comando stesso da eseguire. Un esempio:

5 3 * * * /usr/bin/apt-get update

La prima parte della voce descrive quando l'azione deve essere effettuata. Ci sono cinque campi, separati da uno spazio, ognuno dei quali accetta un numero, un asterisco (*), o un testo appropriato. I campi specificano, in ordine, il *minuto*, l'*ora*, il *giorno del mese*, il *mese* a il *giorno della settimana*. Il campo del mese e quello del giorno della settimana permettono di usare un'abbreviazione, come ad esempio "jan" per "gennaio (January) o "thu" per Giovedì (Thursday).

L'esempio precedente eseguirà "/usr/bin/apt-get update" ogni giorno di ogni mese alle 03:05 (cron utilizza un tempo indicato sul sistema di 24 ore). Puoi fare in modo che cron ti annoi ogni 5 minuti durante il tuo turno di lavoro (9 di mattina - 5 di pomeriggio) con un messaggio:

*/5 9-17 * * mon,tue,wed,thur,fri wall "Siamo ancora qui?"

o ricordarti un compleanno alle 9 di mattina il 10 gennaio ogni anno:

0 9 10 jan * echo "E' il compleanno di tua madre oggi!" > ~/readme

Per vedere il contenuto corrente del tuo file di crontab, esegui:

crontab -l

To edit the file, with the editor specified in your environment (which defaults to vim - :q! is the command to escape without saving if you get stuck and need to read up on it or change your editor), use:

crontab -e

When you exit the editor, the new crontab file will be installed. The file is stored in /var/spool/cron/crontabs but should only be edited via the crontab command.

Further Considerations

The above commands are stored in a crontab file belonging to your user account and executed with your level of permissions. If you want to regularly run a command requiring a greater level of permissions, set up a root crontab file using:

sudo crontab -e

Depending on the commands being run, you may need to expand the root users PATH variable by putting the following line at the top of their crontab file:

PATH=/usr/sbin:/usr/bin:/sbin:/bin

It is sensible to test that your cron jobs work as intended. One method for doing this is to set up the job to run a couple of minutes in the future and then check the results before finalising the timing. You may also find it useful to put the commands into script files that log their success or failure, eg:

echo "Nightly Backup Successful: $(date)" >> /tmp/mybackup.log

For more information, see the man pages for cron and crontab (man is detailed on the BasicCommands page). If your machine is regularly switched off, you may also be interest in at (part of the Ubuntu base install) and anacron (found in the Universe repository) that provide other approaches to scheduled tasks.