Product SiteDocumentation Site

9.7. Scheduling Tasks with cron and atd

cron is the daemon responsible for executing scheduled and recurring commands (every day, every week, etc.); atd is that which deals with commands to be executed a single time, but at a specific moment in the future.
In a Unix system, many tasks are scheduled for regular execution:
By default, all users can schedule the execution of tasks. Each user has thus their own crontab in which they can record scheduled commands. It can be edited by running crontab -e (its content is stored in the /var/spool/cron/crontabs/user file).
The root user has their own crontab, but can also use the /etc/crontab file, or write additional crontab files in the /etc/cron.d directory. These last two solutions have the advantage of being able to specify the user identity to use when executing the command.
Paket cron termasuk secara default beberapa perintah terjadwal yang mengeksekusi:
Many Debian packages rely on this service: by putting maintenance scripts in these directories, they ensure optimal operation of their services.

9.7.1. Format dari sebuah Berkas crontab

Each significant line of a crontab describes a scheduled command with the six (or seven) following fields:
  • nilai untuk menit (angka dari 0-59);
  • nilai untuk jam (dari 0 sampai 23);
  • nilai untuk tanggal (dari 1 sampai 31);
  • nilai untuk bulan (dari 1 untuk 12);
  • the value for the day of the week (from 0 to 7, 1 corresponding to Monday, Sunday being represented by both 0 and 7; it is also possible to use the first three letters of the name of the day of the week in English, such as Sun, Mon, etc.);
  • the user name under whose identity the command must be executed (in the /etc/crontab file and in the fragments located in /etc/cron.d/, but not in the users' own crontab files);
  • the command to execute (when the conditions defined by the first five columns are met).
Semua rincian ini didokumentasikan di halaman manual crontab(5).
Each value can be expressed in the form of a list of possible values (separated by commas). The syntax a-b describes the interval of all the values between a and b. The syntax a-b/c describes the interval with an increment of c (example: 0-10/2 means 0,2,4,6,8,10). An asterisk * is a wildcard, representing all possible values.

Contoh 9.2. Sampel berkas crontab

#men jam tgl bln hrmg  perintah

# Unduh data setiap malam pada pukul 19:25
 25  19   *   *   *    $HOME/bin/

# 8:00, pada hari kerja (Senin sampai Jumat)
 00  08   *   *   1-5  $HOME/bin/lakukansesuatu

# Jalankan ulang proksi IRC setiap setelah reboot
@reboot /usr/bin/dircproxy

9.7.2. Menggunakan Perintah at

The at executes a command at a specified moment in the future. It takes the desired time and date as command-line parameters, and the command to be executed in its standard input. The command will be executed as if it had been entered in the current shell. at even takes care to retain the current environment, in order to reproduce the same conditions when it executes the command. The time is indicated by following the usual conventions: 16:12 or 4:12pm represents 4:12 pm. The date can be specified in several European and Western formats, including DD.MM.YY (27.07.15 thus representing 27 July 2015), YYYY-MM-DD (this same date being expressed as 2015-07-27), MM/DD/[CC]YY (ie., 12/25/15 or 12/25/2015 will be December 25, 2015), or simple MMDD[CC]YY (so that 122515 or 12252015 will, likewise, represent December 25, 2015). Without it, the command will be executed as soon as the clock reaches the time indicated (the same day, or tomorrow if that time has already passed on the same day). You can also simply write “today” or “tomorrow”, which is self-explanatory.
$ at 09:00 27.07.15 <<END
> echo "Don't forget to wish a Happy Birthday to Raphaël!" \
>   | mail
warning: commands will be executed using /bin/sh
job 31 at Mon Jul 27 09:00:00 2015
An alternative syntax postpones the execution for a given duration: at now + number period. The period can be minutes, hours, days, or weeks. The number simply indicates the number of said units that must elapse before execution of the command.
To cancel a task scheduled by cron, simply run crontab -e and delete the corresponding line in the crontab file. For at tasks, it is almost as easy: run atrm task-number. The task number is indicated by the at command when you scheduled it, but you can find it again with the atq command, which gives the current list of scheduled tasks.