<p>I'd like to launch a Kubernetes job and give it a fixed deadline to finish. If the pod is still running when the deadline comes, I'd like the job to automatically be killed.</p>

<p>Does something like this exist? (At first I thought that the Job spec's <code>activeDeadlineSeconds</code> covered this use case, but now I see that <code>activeDeadlineSeconds</code> only places a limit on when a job is re-tried; it doesn't actively kill a slow/runaway job.)</p>

<p>You can self-impose timeouts on the container's entrypoint command by using GNU <code>timeout</code> utility.</p>

<p>For example the following Job that computes first 4000 digits of pi will time out after 10 seconds:</p>

<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    metadata:
      name: pi
    spec:
      containers:
      - name: pi
        image: perl
        command: ["/usr/bin/timeout", "10", "perl", "-Mbignum=bpi", "-wle", "print bpi(4000)"]
      restartPolicy: Never
</code></pre>

<p>(Manifest adopted from <a href="https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/#running-an-example-job" rel="noreferrer">https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/#running-an-example-job</a>)</p>

<p>You can play with the numbers and see it timeout or not. Typically computing 4000 digits of pi takes ~23 seconds on my workstation, so if you set it to 5 seconds it'll probably always fail and if you set it to 120 seconds it will always work.</p>
