
crontab ( クロンタブ 、あるいはクーロンタブ、クローンタブとも)コマンドはUnix系OSにおいて、 コマンドの定時実行のスケジュール管理を行うために用いられるコマンドである。 crontab (Kurontabu, tabs, or could, with crontab) command is Unix based OS in which the command used to perform regular schedule execution of commands. 標準入力からコマンド列を読み取り、crontabと呼ばれるファイルにそれを記録する。 Sequence of commands read from standard input, crontab to record it in a file called. この記録を元に定時になると、その命令内容を読み取り、実行が行われる。 And will be scheduled based on this record, read the contents of that instruction, the execution is done. cronという名称はギリシア語のクロノス ( χρόνος )に由来するという説がある(Command Run ON の略という説も)。 called the word cron Kronos Greek (χρόνος) derived from the theory that there is (Command Run ON the theory that stands for).
一般にcrontabコマンドで編集されたスケジュール内容は、 crond デーモンにより実行される。 General crontab command to edit contents of schedules, crond is executed by the daemon. crondはバックグラウンドで稼動し、毎分ごとに実行すべきスケジュールがないか確認し、もし実行すべきジョブがあれば、それを実行する。 crond is running in the background, make sure you do not have every minute scheduled to be executed, if the job should run if you run it. このジョブは「cron job」とも呼ばれる。 This job is a "cron job" is also called.
目次 Contents |
crontabファイルには、ジョブのリストおよびcronデーモンへの命令が書かれる。 crontab file, cron jobs, and a list of written instructions to the daemon. 各ユーザは自身の個人用crontabファイルを持つ。 Each user has a personal crontab file itself. さらに、システム全体用のcrontabファイルも存在する(/etc またはそのサブディレクトリ下)。 In addition, also present for the entire system crontab file (/ etc or a subdirectory). このシステム全体用のcrontabはシステム管理者のみが編集可能なものとなっている。 Of this system-wide crontab is something that can only be edited by the system administrator.
crontabファイルの各行は、空白またはタブで区切られたフィールド列から構成される特有の形式となっている。 Each line in crontab file, which is unique format string consisting of fields separated by spaces or tabs. 各フィールドには単一もしくは複数の値が書かれる。 Each field has a single or multiple values are written.
一フィールド中で複数の値を指定するには、いくつかの方法がある: To specify multiple values in one field, there are several ways:
cron実装によっては、いくつかの追加拡張をおこなっているものもある。 cron implementation, there are also some additional enhancements Okonatsu. スラッシュ (/) で一定値ごとの間隔を表現する: 例) 時フィールドでの"*/3"指定は"0,3,6,9,12,15,18,21"と同じ意を示す。 Slash (/) to represent the one constant in each interval: eg) in the field when "* / 3" specify "0,3,6,9,12,15,18,21" indicate the same meaning. つまり、"*"の場合は毎時をあらわすが、"/3"を指定すると、*で適用される値の範囲内における1番目・4番目・7番目...といった意を表す。 In other words, "*" represents the case of the hour, "/ 3" is specified, * in the range of values to be applied in one second and the fourth and seventh ... express.
# (行頭の # マークはコメント行を示す) # (# Marks the start of a line indicates a comment line) # +------------ 分 (0 - 59) # +------------ Minutes (0 - 59) # | +---------- 時 (0 - 23) # | +---------- Hour (0 - 23) # | | +-------- 日 (1 - 31) # | | +-------- Sun (1 - 31) # | | | +------ 月 (1 - 12) # | | | +------ 2008 (1 - 12) # | | | | +---- 曜日 (0 - 6) (日曜日=0) # | | | | +---- Day of week (0 - 6) (Sunday = 0) # | | | | | # | | | | | # * * * * * 実行されるコマンド # * * * * * Command to be executed
注: Note:
第6フィールド以降の行の残りの箇所には実行すべきコマンドを指定する。 The six points the rest of the field after the line that specifies the command to be executed.
#================================================================= #================================================= ================ # SYSTEM ACTIVITY REPORTS # SYSTEM ACTIVITY REPORTS # 平日・午前8時-午後5時の活動履歴は20分おき# 土曜日・日曜日の活動履歴は毎時# 平日・午後6時-午前7時の活動履歴は毎時# 平日 18:05 に概要報告を準備する#================================================================= # Am on weekdays 8:00 - 5 pm every 20 minutes during activity history of the activity history of the hour # # Saturday Sunday Weekdays 6:00 - 7 pm weekdays activity history during the 18:05 hour # summary report prepare #=============================================== ================== 0,20,40 8-17 * * 1-5 /usr/lib/sa/sa1 1200 3 & 0,20,40 8-17 * * 1-5 / usr/lib/sa/sa1 1200 3 & 0 * * * 0,6 /usr/lib/sa/sa1 & 0 * * * 0,6 / usr/lib/sa/sa1 & 0 18-7 * * 1-5 /usr/lib/sa/sa1 & 0 18-7 * * 1-5 / usr/lib/sa/sa1 & 5 18 * * 1-5 /usr/lib/sa/sa2 -s 8:00 -e 18:01 -i 3600 -ubcwyaqvm & 5 18 * * 1-5 / usr/lib/sa/sa2-s 8:00-e 18:01-i 3600-ubcwyaqvm &
DISPLAY=:0.0というような環境変数設定を行うか、アプリケーション実行引数に--display:0.0オプションを付けるかである。 at the beginning of crontab DISPLAY=:0.0 I do like that the environment variable settings, the application is running argument --display:0.0 is the option you want. :0.0 は一例であり、どのディスプレイに出力すべきかは、 echo $DISPLAYといったコマンドを実行して確認しておく必要がある。 : 0.0 is an example, which should be output to the display, echo $DISPLAY it is necessary to confirm and execute the command. # ミス: # Miss: 1 2 3 4 5 touch ~/error_`date "+%Y%m%d"`.txt 1 2 3 4 5 touch ~ / error_ `date" +% Y% m% d "`. Txt
この場合、デーモンからは以下のようなエラーメッセージがメール送信される:「/bin/sh: unexpected EOF while looking for `'' 」 In this case, the mail is sent from the daemon error message like the following: "/ bin / sh: unexpected EOF while looking for`'' "
# エスケープしたもの: # Be escaped: 1 2 3 4 5 touch ~/right_$(date +\%Y\%m\%d).txt 1 2 3 4 5 touch ~ / right_ $ (date + \% Y \% m \% d). Txt
# 以下でも動作可能: シングルクオート1 2 3 4 5 touch ~/error_$(date '+%Y%m%d').txt # Can also work in the following: a single-quote 1 2 3 4 5 touch ~ / error_ $ (date '+% Y% m% d'). Txt
# ダブルエスケープ版。 # Double escape edition. 以下のようなファイル名になる: ~/error_\2006\04\03.txt Get the following file name: ~ / error_ \ 2006 \ 04 \ 03.txt 1 2 3 4 5 touch ~/error_$(date '+\%Y\%m\%d').txt 1 2 3 4 5 touch ~ / error_ $ (date '+ \% Y \% m \% d'). Txt
# 夏時間移行時に備える59 1 1-7 4 0 /root/shift_my_times.sh # Prepare for daylight saving time transition 59 1 1-7 4 0 / root / shift_my_times.sh
一見すると、上記は4月第一日曜日の午前1時59分にコマンドshift_my_times.shを実行するように見えるが、そうではなく、4月1日から4月7日までの毎日、および、4月中の日曜日全てで実行されてしまう。 At first glance, the above first four Sunday morning Mon 1:59 minutes shift_my_times.sh command seems to be running, rather, from April 1 every day until April 7 and April All would have been run in Sunday.
これを書き直すとすれば、以下のようにするのが一つの方法である。 If rewrite it, one way is to be as follows.
# 夏時間移行に備える59 1 1-7 4 * test `date +\%w` = 0 && /root/shift_my_times.sh # Prepare for daylight saving time transition 59 1 1-7 4 * test `date + \% w` = 0 & & / root / shift_my_times.sh
# ログファイルに日付を追加* 0,2,4,6,8,10,12,14,16,18,20,22 * * * date >> /var/log/date.log # Add the date to log file * 0,2,4,6,8,10,12,14,16,18,20,22 * * * date>> / var / log / date.log
これは、各偶数時に毎分実行されてしまう。 It would be run every minute during each even-numbered.
この意図を表現するには以下のようにする: The intent is to represent as follows:
# 毎2時間おきに date コマンドを実行0 0,2,4,6,8,10,12,14,16,18,20,22 * * * date >> /var/log/date.log # Every 2 hours every command date 0 0,2,4,6,8,10,12,14,16,18,20,22 * * * date>> / var / log / date.log
# もっと簡潔に書くと: # And write more concisely: 0 */2 * * * date >> /var/log/date.log 0 * / 2 * * * date>> / var / log / date.log
crontab により実行されたコマンドから出力が行われると、cron デーモンは通常その出力結果をユーザにメールで配送する。 When a crontab command executed on the output, cron daemon will be delivered by regular mail its output to the user.
/dev/nullへのリダイレクトとすることでできる。 To suppress the output of certain commands, the output to /dev/null can be redirected to that. crontab 経由の実行コマンド出力のメールを完全に止めるには、実行コマンド全てに、 標準出力を/dev/nullにリダイレクトした上で標準エラー出力も標準出力へのリダイレクトとするような、以下のリダイレクトを加える。 crontab to completely stop the execution of the command output via email, to execute all commands, the output standard /dev/null to redirect standard output and standard error on the output redirected to the redirection of the following add. ただし、何らかのエラーが発生したとしても、何の出力も得られない。 However, even if some error occurs, can not get any output.
>/dev/null 2>&1 > / dev / null 2> & 1
MAILTO="" MAILTO = ""
ジョブのスケジュールを実際にcronデーモンに伝えるにはcrontabコマンドを使う。 Schedule cron job to actually tell the daemon to use the crontab command. crontabファイル(例: example.crontab )を作成して、crontabコマンドの引数として指定する( crontab example.crontab )。 crontab file (example: example.crontab) to create, crontab command as arguments crontab example.crontab もしくは、引数無しで起動した上で、標準入力からコマンドを直接指定することもできる。 Or start over with no arguments, can also be given direct commands from standard input.
| ||||||||||||||||||||||||||
