Product SiteDocumentation Site

9.7. cronatd を使ったスケジューリングタスク

cron is the daemon responsible for executing scheduled and recurring commands (every hour, every day, every week, etc.). atd deals with commands to be executed a single time, but at a specific moment in the future.
Unix システムでは、以下に挙げる多くのタスクが定期的に実行されるよう予定されています。
デフォルトで、すべてのユーザはタスク実行の予定を入れることが可能です。各ユーザは自分専用の crontab を持っており、これを使ってコマンド実行の予定を登録することが可能です。crontab を編集するには crontab -e を実行します (crontab/var/spool/cron/crontabs/user ファイルに保存されます)。
root ユーザは自分専用の crontab を持っていますが、/etc/crontab ファイルを使ったり、追加的な crontab ファイルを /etc/cron.d ディレクトリに置くことが可能です。最後の 2 つの解決策には、コマンドを実行するユーザを明示できるという利点があります。
デフォルトで cron パッケージには、いくつかのスケジュール済みコマンドが含まれています。
多くの Debian パッケージは cron のサービスに頼っています。すなわち、メンテナンススクリプトをこれらのディレクトリに入れて、サービスの最適な動作を保証しています。

9.7.1. crontab ファイルの書式

Each significant line of a crontab entry describes a scheduled command with the six (or seven) following fields:
  • the value for the minute (from 0 to 59);
  • 時間 (0 から 23 までの数字を指定します)。
  • 月の日付 (1 から 31 までの数字を指定します)。
  • 月 (1 から 12 までの数字を指定します)。
  • 曜日 (0 から 7 までの数字、月曜日は 1、日曜日は 0 と 7 の両方、さらに英語で書いた曜日の最初の 3 文字 SunMon などを使うことも可能です)。
  • コマンドを実行するユーザ名 (/etc/crontab ファイルと /etc/cron.d/ にある分割されたファイルでは必要ですが、各ユーザ専用のファイルでは不要です)。
  • 実行するコマンド (最初の 5 つのフィールドで定義された条件が満足されたら実行します)。
すべての詳細は crontab(5) man ページに書かれています。
Each value can also 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.

例 9.2 Sample user crontab file

#Format
#min hour day mon dow  command

# Download data every night at 7:25 pm
 25  19   *   *   *    $HOME/bin/get.pl

# 8:00 am, on weekdays (Monday through Friday)
 00  08   *   *   1-5  $HOME/bin/dosomething

# every two hours
 *  */2   *   *   *    $HOME/bin/dosomethingelse

# Restart the IRC proxy after each reboot
@reboot /usr/bin/dircproxy

9.7.2. 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.22 thus representing 27 July 2022), YYYY-MM-DD (this same date being expressed as 2022-07-27), MM/DD/[CC]YY (i.e., 12/25/22 or 12/25/2022 will be December 25, 2022), or simple MMDD[CC]YY (so that 122522 or 12252022 will, likewise, represent December 25, 2022). 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.22 <<END
> echo "Don't forget to wish a Happy Birthday to Raphaël!" \
>   | mail lolando@debian.org
> END
warning: commands will be executed using /bin/sh
job 1 at Wed Jul 27 09:00:00 2022
与えられた期間だけ実行を先延ばしにする構文もあります。たとえば at now + number period です。period には minuteshoursdaysweeks を指定することが可能です。number は単純に period で指定した単位の量を表し、この量だけコマンドの実行が先延ばしされます。
cron でスケジュールされたタスクを中止するには、単純に crontab -e を実行し、crontab ファイルから対応する行を削除してください。at タスクの場合、削除はとても簡単です。すなわち atrm task-number を実行してください。タスク番号は at コマンドで予定を登録した際に表示されたものです。もう一度確認するには atq コマンドを実行してください。これはスケジュールされたタスクの現在のリストを表示します。