Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cron Job with timeout

Tags:

kubernetes

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.

like image 490
danielinclouds Avatar asked Mar 30 '19 11:03

danielinclouds


People also ask

Does a cron job have a timeout?

But if the network is not good or your cron script is handling a big task, the time that the cron job costs could be long, and once it's longer than a specific time period, EasyCron will abort the current execution of this cron job. This "specific time period" is called cron job timeout limit.

How do I stop a cron job after a certain time?

You can stop a single cron job by removing its line from the crontab file. To do that, run the crontab -e command and then delete the line for the specific task. Alternatively, you can stop the cron job by commenting it out in the crontab file.

What is * * * * * In cron job?

What does * mean in Cron? The asterisk * is used as a wildcard in Cron. * sets the execution of a task to any minute, hour, day, weekday, or month.

Can you run a cron job every 30 seconds?

But still, you can do some tricky configurations to run your script to run at every 30 seconds. In the above configuration, we have scheduled the script twice. The first cron runs every 1 minute and the second cron also starts at the same time but waits for 30 seconds before execution.


2 Answers

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.

like image 163
almalki Avatar answered Sep 18 '22 14:09

almalki


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
like image 39
Alex Mapley Avatar answered Sep 18 '22 14:09

Alex Mapley