danielinclouds
danielinclouds

Reputation: 467

Cron Job with timeout

Is there a way to provide timeout to kubernetes CronJob?

I need to schedule a task that runs according to Cron schedule but I need to limit execution of this task to only 20 seconds. If the task runs longer than 20 seconds than it should be terminated. I tried using .spec.startingDeadlineSeconds but this didn't help.

Upvotes: 32

Views: 27779

Answers (2)

Alex Mapley
Alex Mapley

Reputation: 922

Here's a simple example taken from https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/ and modified with an hour timeout added:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      activeDeadlineSeconds: 3600
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

Upvotes: 10

almalki
almalki

Reputation: 4775

Use cronjob.spec.jobTemplate.spec.activeDeadlineSeconds:

FIELDS:

activeDeadlineSeconds Specifies the duration in seconds relative to the startTime that the job may be active before the system tries to terminate it; value must be positive integer

From documentation:

Another way to terminate a Job is by setting an active deadline. Do this by setting the .spec.activeDeadlineSeconds field of the Job to a number of seconds.

The activeDeadlineSeconds applies to the duration of the job, no matter how many Pods are created. Once a Job reaches activeDeadlineSeconds, all of its Pods are terminated and the Job status will become type: Failed with reason: DeadlineExceeded.

Upvotes: 80

Related Questions