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.
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.
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 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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With