Lullar    


ビデオジャパンは、最大でランク。

crontab crontab

出典: フリー百科事典『ウィキペディア(Wikipedia)』 Source: material may be challenged Wikipedia encyclopedia (Wikipedia) 』

crontabクロンタブ 、あるいはクーロンタブ、クローンタブとも)コマンドはUnixOSにおいて、 コマンドの定時実行のスケジュール管理を行うために用いられるコマンドである。 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ファイル crontab file

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.

[ 編集 ] 特殊記号 special characters

一フィールド中で複数の値を指定するには、いくつかの方法がある: To specify multiple values in one field, there are several ways:

  • コンマ (,) で値のリストを指定する: 例) "1,3,4,7,8" Comma (,) to specify a list of values: eg) "1,3,4,7,8"
  • ダッシュ (-) で値の範囲を指定する: 例) "1-6" ("1,2,3,4,5,6"という指定と同じ意味) Dash (-) to specify the range of values: eg) "1-6" ( "1,2,3,4,5,6" given that the same meaning.)
  • アスタリスク (*) でそのフィールドで取りうる全ての値を表現する。 An asterisk (*) to represent all possible values in its field. 例えば、時をあらわすフィールドでは「毎時」という意味となる。 For example, in representing the field at the "hour" will mean.

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.

[ 編集 ] フィールド Field

# (行頭の # マークはコメント行を示す) # (# 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:

  1. 「曜日」(第5フィールド)では0, 7の両方とも日曜日の意となる。 "Time" (5th field) in 0, 7 and both the meaning of Sunday.
  2. 直感にあわないものの、「日」(第3フィールド)および「曜日」(第5フィールド)が同時に指定された場合、どちらかが満たされた場合両方でコマンドが実行される。 But intuition confront, "Sun" (third field) and "Time" (5th field) is specified at the same time, the command is executed in both must be met either. 以下の例も参照のこと。 See also the following example.

第6フィールド以降の行の残りの箇所には実行すべきコマンドを指定する。 The six points the rest of the field after the line that specifies the command to be executed.

[ 編集 ] Example

[ 編集 ] AIX システムにおけるadm ユーザのcrontabファイル AIX user adm crontab file system

#================================================================= #================================================= ================
# 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 &

[ 編集 ] よくあるミス Common mistakes

  • 最もよく見られるミスの一つは、cron ジョブのテストの際に見られる。 One of the most common mistakes, cron jobs found during the test. テストの際には、少なくとも2分以上先の時刻を実行時刻として指定する必要がある。 During the test, at least two must be designated as the first time in more than a minute running time. これはcrontabファイルの再読み込みは、編集後の次の分時にのみ行われるためである。 This re-read the crontab file is to be done only when the next minute after editing. 例えば、現在時刻が12:05である場合、crontabファイルの再読み込みが12:06:01に行われるため、次のジョブをスケジュールするには少なくとも12:07以降を指定する必要がある。 For example, if the current time is 12:05, crontab file reload is done in 12:06:01, to schedule the next job you need to specify at least 12:07 or later. これに対処してテストをすぐに行うには、cronサービス自体を再起動する方法もある。 To do the test right away to deal with this, cron there are ways to restart the service itself.
  • crontabからX Window Systemアプリケーションを起動するというミスも多く見受けられる。 crontab be seen from the X Window System to start an application that many mistakes. この問題は、cronからはX環境が実行可能かどうかが不明であり、crontabは主としてコンソールのみを対象としたプログラムの実行を意図しているためもあり、Xの情報を受け渡すことができない点にある。 This problem, cron from the X is unknown whether the executable environment, crontab is also intended for the execution of a program primarily aimed at only the console, X-point can not pass information located. これに対応するには2つの方法がある。 To counter this there are two ways. crontabの冒頭に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.
  • よくあるミスのひとつは、コマンド指定においてエスケープせずに「%」記号を使うことで、これはエスケープする必要がある。 One common mistake is to specify in the command does not escape "%" in the use of symbols, which need to be escaped.
# ミス: # 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
  • 以下も別のよくあるエラーのひとつである: Another common error is one of the following:
# 夏時間移行時に備える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
  • また、別のエラーの例としては、2時間毎にジョブを実行しようとして、以下のように書くと、 Also, as another example of an error, try to run a job every two hours, and written as follows
# ログファイルに日付を追加* 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

[ 編集 ] Emailの抑止 Email deterrent

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
  • 現在広く使われているVixie cronでは、crontabファイルの冒頭に以下の記述を加えるだけで、全てのメール通知を止めることができる。 Are now widely used Vixie cron does, crontab just add the following description at the beginning of the file, you can stop all email notifications.
MAILTO="" MAILTO = ""

[ 編集 ] crontabコマンド crontab command

ジョブのスケジュールを実際に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.

[ 編集 ] 関連項目 See also

  • at : ある時刻に一回だけコマンドを実行する at: to run a command once given time
  • anacron : 一定時間の経過を見て、周期的にジョブを実行する anacron: a look at the period of time, to run the job periodically
  • launchd : Mac OS X / Darwin上の cron 代替デーモン launchd: Mac OS X / Darwin for the cron daemon replacement

[ 編集 ] 外部リンク External links


ビデオジャパンは、最大でランク。




© Lullar