This is Gentoo's testing wiki. It is a non-operational environment and its textual content is outdated.

Please visit our production wiki at https://wiki.gentoo.org

cron

From Gentoo Wiki (test)
Jump to:navigation Jump to:search
This page is a translated version of the page Cron and the translation is 81% complete.
Outdated translations are marked like this.
Resources

このガイドでは、Gentoo Linuxにおけるcronデーモンのセットアップと利用方法を説明します。

cronの基礎

cronとは何か

Cron is a daemon that runs scheduled tasks based on input from the command crontab. It accomplishes this task by waking up every minute and checking to see if there are any cron-jobs to run in any of the user crontabs.

Note
Notice that crontab is both the name of a list of cron-jobs as well as the name of the command to edit that list.

デファクトのcron

There are a few cron implementations to choose from in Portage. All of them offer a similar interface, namely the use of crontab or a similar command. There is also a related utility called Anacron which is meant to work with cron on systems that are not continuously running.

また、現在利用可能な全てのcronパッケージがsys-process/cronbaseに依存していることは強調すべきです。技術的には、どのcronパッケージもこのパッケージに依存していません。しかし、このパッケージは大部分のユーザに有益でcronに似た機能を提供します。

cronを動作させる前に、適切なcron実装が選択されていなければなりません。このガイドでは、Gentoo Linuxで利用可能なcron実装を説明します。

どのcronが適しているか?

Note
Emerge virtual/cron to install Gentoo's default cron implementation.

vixie-cron

Vixie-cron is a full featured cron implementation based on SysV cron. Each user has his own crontab and is allowed to specify environment variables within that crontab. Unlike the other cron variants, it also offers support for SELinux and PAM. It supports fewer architectures than Dcron, but more than Fcron. Latest version is 4.1 released on January 2004.

sys-process/vixie-cron の特徴

  • SELinuxのサポート
  • PAMのサポート (/etc/security/limits.conf)
  • crontabで環境変数を設定可 (PATH、SHELL、HOME等)
  • ユーザ独自のcrontabを持つことが可能。cron.allowcron.denyによるアクセス制御。

cronie

cronie (sys-process/cronie)はvixie-cron のフォークで、Fedora で開発されました。このためcronieはオリジナルのvixie-cronと同じ特徴を持ちます。さらに、cronieは anacronの機能も実装しています。この機能を利用するにはanacronUSEフラグを有効にする必要があります。

dcron (Dillonのcron)

Dcron aims to be a simple, elegant and secure implementation of cron. It does not allow the specification of environment variables in crontabs and all cron-jobs are run from /bin/sh. Like vixie-cron, each user has his own crontab. As of version 4 it contains anacron-like features.

sys-process/dcron の特徴

  • Fast, simple and free of unnecessary features;
  • Access to crontab is limited to the cron group, i.e. it doesn't rely on any external faculties.

fcron

fcronはvixie-cronとanacronの置き換えを意図しています。連続稼働しないシステムで動作できるよう設計されていて、いくつかの拡張機能を持っています。それは「ジョブがスタートするための条件設定」、「ジョブのシリアライゼーション制御」、「ジョブにnice値を付与する機能」、「システム起動時に動作するジョブのスケジュール」です。さらに詳細な情報についてはfcron's home pageを参照してください。

sys-process/fcronの特徴

  • 連続稼働しないシステムで動作するように設計されています。つまり、システム停止中のため動作できなかったジョブを再起動後に実行できます。
  • 環境変数やその他多くのオプションをcrontabで設定できます。
  • 多くの新機能と共に、拡張されたcrontab記述をサポートします。
  • 各ユーザーはcron.allowcron.denyでアクセス制御可能なcrontabを持つことができます。

bcron

bronはセキュアなオペレーションを念頭に置いて設計された新しいcronシステムです。これを実現するため、cronシステムはいくつかのプログラムに分割され、それぞれが別のタスクに対して責任を持つことになりました。また、それらタスク間のコミュニケーションは厳格に制御されました。ユーザインターフェースは、よく似たシステム(例 vixie-cron)の単純な置き換えですが、内部の実装は大きく異なっています。より詳細な情報についてはbcronのホームページhttp://untroubled.org/bcronを参照してください。

sys-process/bcronの特徴

  • vixie-cronの単純置き換え
  • マルチプロセス設計
  • 地域毎の夏時間サポート

anacron

anacronはcronデーモンそのものではなく、cronデーモンと組み合わせて動作します。anacronは指定された間隔でコマンドを実行し、システムが連続的に稼働することを前提とせず、シャットダウンのために実行されなかったジョブをリブート後に改めて実行します。通常、anacronは毎日動作するcronデーモンに依存しています。

cronを使う

インストール

目的にあった適切なcronを選択し、emergeしてください。

root #emerge --ask dcron

選択されたcronデーモンがシステムのinitプロセスに追加されたことを確認してください。追加されていない場合、cronデーモンはジョブを開始しません。

root #/etc/init.d/dcron start
root #rc-update add dcron default

Optionally, if Fcron or dcron have not been installed, installing Anacron as a helper to the cron daemon might be a wise choice.

root #emerge --ask anacron

anacronをインストールした場合は、anacronをシステムのinitプロセスに確実に加えてください。

root #/etc/init.d/anacron start
root #rc-update add anacron default

For anacron, there is usually no init process. Instead, anacron needs to be launched through a different cron implementation.

One method is to launch anacron through a cron definition. By default, it installs an hourly run script, which is consumed by most cron implementations by default. If that isn't the case though, then it can still be launched through manual definitions:

FILE /etc/crontabLaunching anacron through a cron definition
# Start anacron every 10 minutes
*/10 * * *  root  /usr/sbin/anacron
 
# Alternatively, run the anacron-provided 0anacron script every hour
# 59 * * * *  root  /etc/cron.hourly/0anacron

システムcrontab

The post install messages from some of these cron packages instruct the user to run crontab /etc/crontab. The /etc/crontab file is the system crontab. A cron installation can use it in conjunction with sys-process/cronbase to run the scripts in /etc/cron.{daily,hourly,weekly,monthly}. Note that only vixie-cron and cronie schedule jobs in /etc/crontab automatically. Dcron and fcron users will need to run crontab /etc/crontab every time they make changes to the /etc/crontab file.

Please note that jobs scheduled in the system crontab might not show up in the list of cron-jobs displayed by running crontab -l.

Of course, users can choose not to use any system crontab at all. If dcron or fcron has been chosen, do not run crontab /etc/crontab. If vixie-cron, cronie or bcron has been chosen comment all lines in /etc/crontab.

sedコマンドでファイルの全ての行をすばやく簡単にコメントアウトできます。etc/crontabに対して次のコマンドを実行してください。

root #sed -i -e "s/^/#/" /etc/crontab

信頼できるユーザーにcronへのアクセス権を与える

root以外でcronデーモンにアクセスする場合は、このセクションが参考になるでしょう。そうでない場合は次のセクションScheduling cron-jobsに進んで下さい。

Note
Giving another user access to crontab does not let him run cron-jobs as root. For a user to be able to edit the root crontab, look into using sudo (app-admin/sudo). Please read the Gentoo Sudo(ers) Guide for more details.

選択したcronパッケージに関わらず、あるユーザーにcrontabへのアクセスを許可する場合、そのユーザはcronグループに所属しなければなりません。例として、次のコマンドはユーザ"wepy"をcronグループに追加します。

root #gpasswd -a wepy cron
注意
ユーザーをcronグループに追加した際は、そのユーザは一旦ログアウト後、再度ログインする必要があります。

dcronの場合、上記の手順だけでユーザーはcrontabにアクセスできるようになります。よってdcronユーザは次のセクションScheduling cron-jobsに進んでください。その他のユーザーはこのまま本セクションを読み進めてください。

fcronの場合は/etc/fcron/fcron.deny/etc/fcron/fcron.allowを編集します。最もシステムを安全に保つ方法は、最初に/etc/fcron/fcron.denyで全てのユーザーを拒否して、次に明らかに許可したいユーザーのみ/etc/fcron/fcron.allowで許可することです。

重要
もし、/etc/fcron/fcron.allow/etc/fcron/fcron.denyも存在しない場合、cronグループに所属する全てのユーザーがcrontabの使用を許可されます。fcronのデフォルトはfcron.allowallows all usersを記述した状態であり、cronグループに所属しているユーザーはfcrontabにアクセスできます。
CODE fcron.denyで全てのユーザーを拒否
all

もし、あるユーザー(ここで再度wepyを例にとります)が独自のcronジョブをスケジュールしたい場合、次を/etc/fcron/fcron.allowに追加してください。

CODE fcron.allowでユーザーwepyを許可
wepy

vixie-croncronieの場合は、単に/etc/cron.allowを編集します。

重要
もし/etc/cron.allowしか存在しない場合、同ファイルに記載されているcronグループのユーザーのみがアクセス可能です。逆にもし空の/etc/cron.denyのみが存在する場合、すべてのcronグループユーザがアクセス可能になります。そのため、/etc/cron.allowが存在しない場合は、絶対に/etc/cron.denyを空にしてはいけません。

例として、ユーザー"wepy"にアクセス権を与える場合、/etc/cron.allowにwepyを追加します。

CODE /etc/cron.allowでユーザwepyを許可
wepy

cronジョブのスケジューリング

crontabを編集する手順はcronパッケージ毎に異なります。しかしそれらすべては同じ基本コマンド群をサポートしています。例えばcrontabの作成、置き換え、編集、消去、crontab中のジョブのリストアップといったことを実行します。次のリストは、それぞれのパッケージ毎にどのようにコマンドを実行するかを示しています。

Version Edit crontab Remove crontab New crontab List cron-jobs
dcron crontab -e crontab -d [user] crontab file crontab -l
fcron fcrontab -e fcrontab -r [user] fcrontab file fcrontab -l
vixie-cron, cronie & bcron crontab -e crontab -r -u [user] crontab file crontab -l
注意
引数無しで消去コマンドを使用すると、現在のユーザのcrontabを削除します。
注意
fcronの場合、fcrontabはcrontabのシンボリックリンクです。

これらのコマンドを実施する前に、crontabそのものを理解することがまず必要です。crontab中のそれぞれの行は、5つのフィードを次の順序で持ちます。分(0~59)、時間(0~23)、日(1~31)、月(1~12)、曜日(0~7、月曜日が1で日曜日が0もしくは7)。曜日と月は3文字の略称で表すこともできます。例えばmon、tue、jan、feb等です。それぞれのフィールドは範囲で表すこともできます。例えば1-5やmon-friといった記述です。カンマで分離された記述(例:1,2,3やmon,tue,wed)や刻み幅を使った範囲指定(例:1-6/2は1と3と5を示します)も可能です。

少し混乱するかもしれませんが、いくつかの記述例を見ればそれほど複雑でないことがわかるでしょう。

CODE
# /bin/falseを年中にわたり毎分に実行
*     *     *     *     *        /bin/false
  
# /bin/falseを月・火・水曜日と毎月4日それぞれの1時35分に実行
35    1     4     *     mon-wed  /bin/false
  
# /bin/true を3月2日の22:25に実行
25    22    2     3     *        /bin/true
  
# /bin/false を月・水・金曜の2時0分に実行
0     2     *     *     1-5/2    /bin/false
注意
日と曜日を組み合わせて記述する場合、もし日と曜日のどちらかを*にした場合、*でない方の記述が優先されます。逆に日と曜日の両方を*にした場合、それは毎日実行されることを意味します。

何がどう影響するのかをテストするために数個のcronジョブで確認しましょう。最初に次のファイルcrons.cronを作成します。

FILE crons.croncrons.cronファイルを作成する
#Mins  Hours  Days   Months  Day of the week
10     3      1      1       *       /bin/echo "I don't really like cron"
30     16     *      1,2     *       /bin/echo "I like cron a little"
*      *      *      1-12/2  *       /bin/echo "I really like cron"

このcrontabファイルを上のコマンドリストの"crontab新規作成"コマンドを使って作成しましょう。

root #crontab crons.cron
注意
echoコマンドの出力はリダイレクトしない限り見ることができません。

スケジュールされたcronジョブを確認するため、コマンドリストの"cronジョブの表示 "に記載されているコマンドを使用します。

root #crontab -l

crons.cronとよく似たリストが得られるはずです。表示されない場合は、おそらく適切でないコマンドをcrontabに与えたのでしょう。

このcrontabは2ヶ月毎に、毎日、毎時間、毎分、"I really like cron"を出力するはずです。明らかにユーザはcronが気に入ったときにそれをするのでしょう。このcrontabは1月と2月の毎日16時30分に、"I like cron a little"を出力します。また1月1日の3時10分に"I don't really like cron"を出力します。

もしanacronを使っている場合は、このままこのセクションを読み続けてください。もしanacronを使っていない場合は、次のセクションEditing crontabsに進んでください。

anacronユーザは/etc/anacrontabを編集します。このファイルは4つのフィードを持っています。それぞれ「何日毎に実行するか」、「anacron実行の何分後にジョブを実行するか」、「ジョブの名前」、「ジョブの実行コマンド」です。 

For example, to have it run echo "I like anacron" every 5 days, 10 minutes after anacron is started, enter the following:

FILE /etc/anacrontab
5 10 wasting-time /bin/echo "I like anacron"

anacronはanacrontabファイルに記載のすべてのジョブを完了後、終了します。これらのジョブが毎日実行されるかどうかを確認するためにcronデーモンが使用されます。次のセクションの最後に、これがどのように扱われるかを記載します。

crontabの編集

現実的に、ユーザーがどれぐらいcronを好きかをシステムに逐一報告させることを希望するユーザーはいないでしょう。次のステップとして、上記のコマンドリストの消去コマンドに相当するものを使用して、これまで使用したサンプルのcrontabを消去しましょう。消去した後はリストコマンドを使用して、cronジョブがすでに動作していないことを確認しましょう。

root #crontab -d
root #crontab -l

No cron-jobs should be displayed in the output from crontab -l. If cron jobs are listed, then the remove command failed to remove the crontab; verify the correct remove command for the system's cron package.

Now that we have a clean state, let's put something useful into the root crontab. Most people will want to run updatedb on a weekly basis to make sure that mlocate works properly. To add that to the system's crontab, first edit crons.cron again so that it looks like the following:

CODE 実際のcrontab
22 2 * * 1    /usr/bin/updatedb

この例では、cronは毎週月曜朝の午前2時22分にupdatedbを起動します。適切な新規作成コマンドを上のコマンドリストから選択、実行してください。実行後、もう一度リストをチェックしましょう。

root #crontab crons.cron
root #crontab -l

Now let's say emerge --sync should be ran on a daily schedule in order to keep the Portage tree up to date. This could be done by first editing crons.cron and then using crontab crons.cron as was done in the example above, or by using the proper edit command from the table above. This provides a way to edit the user's crontab in situ, without depending on external files like crons.cron.

root #crontab -e

The above command should open the user's crontab with an editor. For example, if emerge --sync is to be run every day at 6:30 A.M., make the crontab look something like this:

CODE 実際のcrontab
22 2 * * 1    /usr/bin/updatedb
30 6 * * *    /usr/bin/emerge --sync
## (anacronを使う場合は以下を加えてください)
30 7 * * *    /usr/sbin/anacron -s

ジョブがスケジュールされたことを今一度確認するため、前述の例のようにcronジョブのリストを確認してください。もし期待しているものが全て表示された場合、ロックンロールの準備OKです。

cronbaseを使う

最初に述べたように、すべてのcronパッケージはsys-process/cronbaseに依存しています。このcronbaseパッケージは/etc/cron.{hourly,daily,weekly,monthly}と、run-cronsと呼ばれるスクリプトを生成します。デフォルトの/etc/crontabは以下のようになっていることに注意してください。

CODE デフォルトのシステムcrontab
*/15 * * * *     test -x /usr/sbin/run-crons && /usr/sbin/run-crons
0  *  * * *      rm -f /var/spool/cron/lastrun/cron.hourly
0  3  * * *      rm -f /var/spool/cron/lastrun/cron.daily
15 4  * * 6      rm -f /var/spool/cron/lastrun/cron.weekly
30 5  1 * *      rm -f /var/spool/cron/lastrun/cron.monthly

このファイルの詳細に立ち入るのはやめて、これらのコマンドは毎時、毎日、毎週、毎月スクリプトを実行するものとしましょう。cronジョブをスケジューリングするためのこの方法にはいくつかの重要なメリットがあります。

  • これらのジョブがスケジュールされた時刻にコンピューターがオフだったとしても、再起動後にジョブは実行されます。
  • パッケージのメンテナーにとって、明確に定義された場所にスクリプトを設置することは容易です。
  • 管理者はcronジョブとcrontabがどこに保存されているか、正確に知ることが可能です。これにより、これらシステム構成物のバックアップおよびリストアが容易になります。
注意
vixie-cron、cronie、bronは自動的に/etc/crontabを読み込みます。一方、dcronとfcronは自動的には読み込みません。この点については/etc/crontabセクションを熟読してください。

anacronを使う

先に記述したとおり、anacronは(ほとんどのデスクトップインストールがそうであるように)連続稼働しないシステムで使用されます。anacronのデフォルトの設定ファイルは/etc/anacrontabであり、通常は以下のようになります。

FILE /etc/anacrontab
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# フォーマット: 間隔 遅延 ジョブ識別子 コマンド
1       5       cron.daily      run-parts /etc/cron.daily
7       10      cron.weekly     run-parts /etc/cron.weekly
30      15      cron.monthly    run-parts /etc/cron.monthly

anacrontabと他の一般的なcrontabとの違いは、anacronはジョブをスケジュールするにあたり、ランとランの間の時間間隔のみ設定し、固定した日時を指定しないことです。anacron起動時、anacronは/var/spool/anacronの内容をチェックして、最後のランから期限が切れたエントリーがないかどうか計算します。もしこのようなエントリが存在した場合、そのコマンドは再度起動されます。

最後の留意点として、システムにインストールされた他のcronとオーバーラップするエントリはコメントアウトしなければなりません。例えば以下のvixie-cronのcrontabはその例です。

FILE /etc/crontab
# for vixie-cron
# $Header: /var/cvsroot/gentoo-x86/sys-process/vixie-cron/files/crontab-3.0.1-r4,v 1.3 2011/09/20 15:13:51 idl0r Exp $
  
# Global variables
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
  
# check scripts in cron.hourly, cron.daily, cron.weekly and cron.monthly
59  *  * * *    root    rm -f /var/spool/cron/lastrun/cron.hourly
#9  3  * * *    root    rm -f /var/spool/cron/lastrun/cron.daily
#19 4  * * 6    root    rm -f /var/spool/cron/lastrun/cron.weekly
#29 5  1 * *    root    rm -f /var/spool/cron/lastrun/cron.monthly
*/10  *  * * * root    test -x /usr/sbin/run-crons && /usr/sbin/run-crons @hourly root nice -n 19 run-parts --report /etc/cron.hourly

オーバーラップをコメントアウトしない場合、日、週、月の部分は、cronデーモンとanacronによって異なる時間に実行されます。これにより二重にジョブが実行されるかもしれません。


Troubleshooting

cronが正しく動作しない場合、以下のチェックリストが役に立つかもしれません。

Remember, each cron package is different and the range of features varies greatly. Be sure to consult the man pages for crontab, fcrontab, or anacrontab, depending on which cron daemon has been activated!

cronは起動していますか?

cronが起動しているかどうかを検証するため、cronがプロセスリストに存在しているかどうかを確認してください。

root #ps ax | grep cron

cronは動作していますか?

次を試してみてください。

CODE cronが動作しているかどうかを確認するためのcrontab
* * * * * /bin/echo "foobar" >> /file_you_own

上記を実施後、/file_you_ownが定期的に更新されているか確認してください。

そのコマンドは動作していますか?

同様に、標準エラー出力もリダイレクトしてみましょう。

CODE アプリケーションの動作を確認するためのcrontab
* * * * * /bin/echo "foobar" >> /file_you_own 2>&1

cronはジョブを起動していますか?

cronのログを確認してください。通常、エラーは/var/log/cron.logもしくは/var/log/messagesに記録されます。

dead.letterはありますか?

通常、cronは何か問題が発生した時はメールを送付します。メールが届いていないか、もしくは~/dead.letterが生成されていないか確認してください。

Why are cron mails not sent out?

In order to receive mails from cron, a valid MTA setup must be implemented. This is provided by any package from virtual/mta.

If the cron mails are only to be sent locally, and not through a fully configured mail server, the system can use mbox (/var/spool/mail) mails, by enabling the mbox useflag with the respective package which provides the MTA.

cronの代替

いくつかのホスティング会社はcronへのアクセスを許可していません。ただし、有償、無償を問わず、多くのcron代替をみつけられるでしょう。

  • EasyCron
    This page is based on a document formerly found on our main website gentoo.org.
    The following people contributed to the original document: Eric Brown, Xavier Neys, Joshua Saddler (nightmorph)
    They are listed here because wiki history does not allow for any external attribution. If you edit the wiki article, please do not add yourself here; your contributions are recorded on each article's associated history page.