<p>I have a use case that my  &quot;./main&quot; binary should run inside the pod and stop after some time (90 seconds) before launching a new pod by the cronJob object.</p>
<p>But I am not confused about how to add both sleep and run my binary in the background together. Please suggest a good approach to this and excuse me for any wrong syntax.</p>
<p>Dockerfile</p>
<pre><code>FROM golang:alpine
WORKDIR /app
COPY main /app
RUN  apk update &amp;&amp; apk add bash
CMD  [&quot;./main &amp;&quot;]

---
cronjob.yaml

apiVersion: batch/v1
kind: CronJob
metadata:
  name: cron
  namespace: test-cron
spec:
  schedule: &quot;*/2 * * * *&quot;
  concurrencyPolicy: Replace
  successfulJobsHistoryLimit: 0
  failedJobsHistoryLimit: 0
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          volumes:
          - name: log
            hostPath:
                path: /data/log/test-cron/
          containers:
            - name: test-cron
              image:  test-kafka-0.5
              command: [&quot;sleep&quot;, &quot;90&quot;] // By adding this, the sleep command is working but my binary is not running inside my container.
</code></pre>

Deepak Mourya
<p>Kubernetes has <a href="https://kubernetes.io/docs/concepts/workloads/controllers/job/#job-termination-and-cleanup" rel="nofollow noreferrer">built-in support for killing a Pod after a deadline</a>; you do not need to try to implement this manually.</p>
<p>In your Dockerfile, set up your image to run the program normally.  Do not try to include any sort of &quot;sleep&quot; or &quot;kill&quot; option, and do not try to run the program in the background.</p>
<pre class="lang-bash prettyprint-override"><code>CMD [&quot;./main&quot;]
</code></pre>
<p>In the Job spec, you need to set an <code>activeDeadlineSeconds:</code> field.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
kind: CronJob
spec:
  jobTemplate:
    spec:
      activeDeadlineSeconds: 90  # &lt;-- add
      template:
        spec:
          containers:
            - name: test-cron
              image:  test-kafka-0.5
              # do not override `command:`
</code></pre>
<p>An identical option exists at the Pod level.  For your use case this doesn't especially matter, but if the Job launches multiple Pods then there's a difference of whether each individual Pod has the deadline or whether the Job as a whole does.</p>
<p>It looks from the question like you're trying to run this job every two minutes and not have two concurrent copies of it.  Assuming the Pod is created and starts promptly, this should accomplish that.  If you had a reason to believe the Job would run faster the second time and just want to restart it, the CronJob might not be the setup you're after.</p>
