このコンテンツは選択した言語では利用できません。

Chapter 34. Cron Jobs


34.1. Overview

A cron job builds on a regular job by allowing you to specifically schedule how the job should be run. Cron jobs are part of the Kubernetes API, which can be managed with oc commands like other object types.

Note

As of OpenShift Container Platform 3.3.1, Cron Jobs is a feature in Technology Preview.

Warning

A cron job creates a job object approximately once per execution time of its schedule, but there are circumstances in which it will fail to create a job or two jobs might be created. As a result, jobs should be idempotent.

34.2. Creating a Cron Job

A cron job configuration consists of the following key parts:

  • A schedule specified in cron format.
  • A job template used when creating the next job.
  • An optional deadline (in seconds) for starting the job if it misses its scheduled time for any reason. Missed jobs executions will be counted as failed ones. If not specified, there is no deadline.
  • ConcurrencyPolicy: An optional concurrency policy, specifying how to treat concurrent jobs within a cron job. Only one of the following concurrent policies may be specified. If not specified, this defaults to allowing concurrent executions.

    • Allow allows Cron Jobs to run concurrently.
    • Forbid forbids concurrent runs, skipping the next run if the previous has not finished yet.
    • Replace cancels the currently running job and replaces it with a new one.
  • An optional flag allowing the suspension of a cron job. If set to true, all subsequent executions will be suspended.

The following is an example of a CronJob resource:

apiVersion: batch/v2alpha1
kind: CronJob
metadata:
  name: pi
spec:
  schedule: "*/1 * * * *"  1
  jobTemplate:             2
    spec:
      template:
        metadata:
          labels:          3
            parent: "cronjobpi"
        spec:
          containers:
          - name: pi
            image: perl
            command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
          restartPolicy: OnFailure 4
  1. Schedule for the job. In this example, the job will run every minute.
  2. Job template. This is similar to the job example.
  3. Sets a label for jobs spawned by this cron job.
  4. The restart policy of the pod. This does not apply to the job controller. See Known Issues and Limitations for details.
Note

All cron job schedule times are based on the timezone of the master where the job is initiated.

You can also create and launch a cron job from a single command using oc run. The following command creates and launches the same cron job as specified in the previous example:

$ oc run pi --image=perl --schedule='*/1 * * * *' \
    --restart=OnFailure --labels parent="cronjobpi" \
    --command -- perl -Mbignum=bpi -wle 'print bpi(2000)'

With oc run, the --schedule option accepts schedules in cron format.

Note

When creating a cron job, oc run only supports the Never or OnFailure restart policies (--restart).

Tip

Delete cron jobs that you no longer need:

$ oc delete cronjob/<cron_job_name>

Doing this prevents them from generating unnecessary artifacts.

34.3. Cleaning Up After a Cron Job

Cron jobs can leave behind artifact resources such as jobs or pods. Check if any remain:

$ oc get jobs
$ oc get pods

All artifacts left over from a job execution use the job name as their prefix. For example, given the cron job example:

$ oc get jobs
NAME            DESIRED   SUCCESSFUL   AGE
pi-1497848100   1         1            1m
pi-1497848160   1         1            49s

$ oc get pods
NAME                  READY     STATUS      RESTARTS   AGE
pi-1497848100-lxs4k   0/1       Completed   0          2m
pi-1497848160-6r0c8   0/1       Completed   0          59s

Delete each artifact if you no longer need them. To delete all jobs spawned by a cron job, specify the label set during cron job creation:

$ oc delete jobs -l <label>

For example, to delete only the jobs generated by the cron job example:

$ oc delete jobs -l parent=cronjobpi
job "pi-1497848100" deleted
job "pi-1497848160" deleted
Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.