⏲️ Cron式生成ツール

オンラインでcron式を生成して検証します。

5.0 / 5 (1 評価)
217 回使用 (30d)

形式: 分 時 日 月 曜日(0-6)

実行説明

一般的なプリセット

使用手順

✓ Cron式の説明:

- : 0-59

- : 0-23

- : 1-31

- : 1-12

- 曜日: 0-6 (0=日曜日, 6=土曜日)

✓ 使用例:

- * * * * * = 毎分実行

- 0 9 * * 1-5 = 平日の朝9時実行

✓ プリセットテンプレートをサポート

✓ 自動バリデーション

Error:

使い方

機能

  • ✓ Cron式を生成
  • ✓ ビジュアルCronビルダー
  • ✓ 次回実行時刻のプレビュー
  • ✓ 一般的なパターン
  • ✓ 式の検証

ステップ

  1. ビジュアルビルダーを使用してスケジュールを設定
  2. またはcron式を直接入力
  3. 次の実行時刻を表示
  4. 一般的なパターンでテスト
  5. 生成された式をコピー

📚 完全ガイド

cron式とは

cron式(cron expression)は、Unix系オペレーティングシステムの「cron」スケジューラで使用される文字列フォーマットです。タスク(コマンドやスクリプト)を自動的に実行する日時を、コンパクトな形式で定義します。このオンラインツールは、そのcron式を視覚的に理解し、生成・検証することを目的としています。

ツールの目的

cron式は強力ですが、その構文は初心者にとっては難解です。このツールの主な目的は、その障壁を取り除くことです。具体的には以下の問題を解決します。

  • 解読の支援: 既存のcron式を入力すると、それが意味する「次回の実行日時」や「スケジュール内容」を平易な言葉で説明します。
  • 作成の支援: チェックボックスやプルダウンメニューといった直感的なインターフェースを操作するだけで、正しいcron式を簡単に構築できます。
  • 検証の支援: 作成または入力したcron式が文法として正しいかどうかを即座にチェックし、誤りがあれば指摘します。

主な機能

このオンラインツールは、cron式に関連する以下の主要な機能を提供します。

  • cron式の解析と説明: 「0 0 * * 1」のような式を入力すると、「毎週月曜日の午前0時0分に実行」といった自然言語での説明を表示します。
  • ビジュアルcron式ジェネレーター: 分、時間、日、月、曜日の各フィールドを個別に設定することで、対応するcron式を自動生成します。複雑な範囲(例: 月曜から金曜)やステップ値(例: 2時間ごと)の設定も可能です。
  • 実行予定のプレビュー: 生成されたcron式に基づいて、次にタスクが実行される日時を一覧表示します。これにより、意図したスケジュールになっているかを確認できます。
  • 一般的なプリセット例: 「毎日深夜」、「営業時間中毎時」、「毎月1日」など、よく使われるスケジュールの例をプリセットとして提供し、学習や出発点として利用できます。

cron式の構成フィールド

cron式は通常、空白で区切られた5つまたは6つのフィールドで構成され、左から順に以下の時間単位を表します。

  • (0 - 59)
  • (0 - 23)
  • (1 - 31)
  • (1 - 12 または JAN-DEC)
  • 曜日 (0 - 6 または SUN-SAT。0と7はどちらも日曜日)
  • (オプションフィールド、1970 - 2099)

各フィールドでは、アスタリスク(*)(全ての値)、カンマ(,)(値のリスト)、ハイフン(-)(値の範囲)、スラッシュ(/)(ステップ値)などの特殊文字を使用して柔軟なスケジュールを定義できます。

cron式を使用する理由

  • 柔軟なスケジューリング

    複雑な実行パターンを簡潔な文字列で定義できます。例えば、「毎営業日の午前9時と午後5時にバックアップを実行」や「毎月第1月曜日の深夜にレポートを生成」といった設定が可能です。
  • システム自動化の基盤

    定期的なメンテナンスタスク(ログローテーション、データベースの最適化、一時ファイルの削除)を自動化し、運用負荷を大幅に軽減します。
  • バッチ処理の効率化

    深夜やトラフィックの少ない時間帯に、大量のデータを処理するバッチジョブ(日次売上集計、メール配信、データ同期)をスケジュールできます。
  • 定期的な通知・リマインダー

    システム監視アラートの定時送信、ユーザー向けの定期メールマガジン配信、またはプロジェクト管理ツールでの定例リマインダー作成に活用できます。
  • アプリケーション機能の定期実行

    Webアプリケーションにおいて、キャッシュの更新、仮想通貨のレート取得、SNSへの自動投稿など、特定の機能を決まった間隔で実行するために使用されます。
  • 開発・テスト環境の管理

    毎晩、最新の本番データを使ってテスト環境をリフレッシュしたり、週末に自動テストスイートを実行したりするなど、開発ライフサイクルを支援します。

スケジュールの詳細化と具体化

曖昧な表現は予期せぬ実行を招く可能性があります。例えば「毎日」を意味する「0 0 * * *」ではなく、具体的な時間「0 5 * * *」(毎日午前5時)を指定します。深夜帯のバッチ処理はシステム負荷が低い時間を狙い、「0 2 * * *」のように明確に定義することが推奨されます。

複雑なスケジュールの分割

単一の複雑な式よりも、複数のシンプルな式に分割することで可読性と保守性が向上します。例えば、「毎週月曜から金曜の9時と17時に実行」という要件は、以下の2つに分けることができます。

  • 0 9 * * 1-5 (平日の9時)
  • 0 17 * * 1-5 (平日の17時)

ステップ値と範囲の活用

ステップ値(/)を効果的に使用することで、定期的な間隔での実行を簡単に定義できます。

  • 「*/15 * * * *」:15分毎に実行。
  • 「0 */3 * * *」:3時間毎に実行(0分時点)。
  • 「0 0 */5 * *」:5日毎に深夜0時に実行。

ワーキングデイ(平日)の指定

月曜から金曜の実行は「1-5」で指定します。土日祝日を考慮する必要がある場合は、cron式のみでは不十分なため、ジョブ内部で休日判定ロジックを実装するか、より高度なスケジューラの使用を検討してください。

タイムゾーンの明示的設定

サーバーのデフォルトタイムゾーンに依存せず、アプリケーションまたはcronデーモン(例:システムcrontabの「CRON_TZ」環境変数)で明示的にタイムゾーンを設定します。これにより、夏時間(DST)やサーバー移行時の予期せぬ動作を防ぎます。

最終日処理の注意点

「L」指定子(最後の日)は全てのcron実装でサポートされているわけではありません。月末処理を実装する場合は、より互換性の高い「0 0 28-31 * *」と組み合わせ、ジョブ内で翌日が月の1日かどうかをチェックするロジックを追加する方法が堅牢です。

テストと検証の徹底

本番環境投入前には、必ず開発環境でスケジュールを検証します。オンラインツールや「cron-expression」パーサーライブラリを使用して、次の数回の実行日時を確認し、意図した通りに動作するかをテストします。

ロギングと監視の実装

cronジョブの実行開始時と終了時には必ずログを出力し、成功・失敗を明確に記録します。ジョブが長時間化したり失敗した場合に検知できるよう、監視システムとの連携やアラート設定を構築することが重要です。

リソース競合の回避

同時に複数の重いジョブが起動しないよう、実行時間を分散させます。特に「0分」や「0時」などは多くのジョブが集中しがちなため、意図的に数分ずらしたスケジュール(例:3, 18, 33, 48分)を検討します。

設定のドキュメント化

cron式だけでは分かりにくいビジネスロジックや、特定の日時を除外する理由などをコメントとして記載します。システムのcrontabファイルやジョブ管理コンソール内で、設定内容と目的を明確に文書化しておきます。

What is a cron expression?

A cron expression is a string consisting of five or six fields separated by white space. It represents a set of times for scheduling tasks, typically used in Unix-like operating systems and various software applications. The fields represent, in order: minutes (0-59), hours (0-23), day of the month (1-31), month (1-12 or names), and day of the week (0-7 or names, where 0 and 7 are Sunday). An optional sixth field can represent seconds. For example, the expression "0 2 * * *" would schedule a task to run at 2:00 AM every day.

What do the asterisk (*) and question mark (?) mean in a cron expression?

The asterisk (*) is a wildcard that means "every" for the given field. For instance, an asterisk in the "hours" field means the task runs every hour. The question mark (?) is used only in the "day of month" and "day of week" fields. It means "no specific value" and is used when you need to ignore one of these two fields. Since the day of the month and day of the week are interdependent, you typically specify a value in one and use a ? in the other to avoid confusion.

How do I schedule a job to run on weekdays only?

To schedule a task to run Monday through Friday (weekdays), you use the "day of week" field. The common expression is "0 9 * * 1-5". This breaks down to: minute 0, hour 9 (9 AM), every day of the month, every month, and days 1 through 5 (Monday to Friday). Note that in some systems, days may be numbered 0-6 (Sunday-Saturday), so 1-5 would then represent Monday-Friday. Always check your specific cron implementation's documentation.

What is the difference between slash (/) and comma (,) in cron syntax?

The slash (/) is used for specifying increments or steps. For example, "*/15" in the minutes field means "every 15 minutes" (i.e., at 0, 15, 30, and 45 minutes past the hour). The comma (,) is used to specify a list of values. For instance, "0 8,12,17 * * *" would run a task at 8 AM, 12 PM, and 5 PM every day. You can combine them: "0 0,12 */2 * *" would run at midnight and noon every two days.

Can I use month or day names in a cron expression?

Yes, many cron implementations allow you to use three-letter abbreviations for months and days of the week. For example, you can write "0 0 1 JAN,APR,JUL,OCT *" to run a task on the first day of January, April, July, and October. Similarly, "0 9 * * MON-FRI" is equivalent to "0 9 * * 1-5" for weekdays. This can make expressions more readable. However, always verify that your specific system (like your server's cron daemon or your programming library) supports this non-numeric format.

How do I schedule a task for the last day of the month?

Scheduling for the last day of the month requires a special notation, as the day number varies (28, 29, 30, or 31). The most reliable method is to use the "L" character in the "day of month" field. The expression "0 0 L * *" would run at midnight on the last day of every month. Some advanced cron systems also support "W" for the nearest weekday and "#" for specifying instances like "the second Friday" (e.g., "0 0 * * 5#2"). Check your cron version for support of these special characters.

Where can I test and validate my cron expressions?

You can test and validate cron expressions using various online tools and libraries. Many websites offer cron expression generators and validators where you can input your expression and see a human-readable description of the schedule (e.g., "At 05:30 on every day-of-month from 1 through 7."). For development, programming languages like Python, JavaScript (Node.js with libraries like `node-cron`), and Java (with `Quartz` scheduler) also have built-in or library-based validators to parse expressions and calculate the next execution times.

関連ツール