<p>I would like to run a shell script inside the Kubernetes using CronJob, here is my CronJon.yaml file :</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: &quot;*/1 * * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - /home/admin_/test.sh
          restartPolicy: OnFailure
</code></pre>
<p>CronJob has been created ( kubectl apply -f CronJob.yaml )
when I get the list of cronjob I can see the cron job ( kubectl get cj ) and when I run &quot;kubectl get pods&quot; I can see the pod is being created, but pod crashes.
Can anyone help me to learn how I can create a CronJob inside the Kubernetes please ?</p>
<p><a href="https://i.stack.imgur.com/JvQQ3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JvQQ3.png" alt="enter image description here" /></a></p>
<p><a href="https://i.stack.imgur.com/lVneg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lVneg.png" alt="enter image description here" /></a></p>

Amin Pashna
<p>As correctly pointed out in the comments, you need to provide the script file in order to execute it via your <code>CronJob</code>. You can do that by mounting the file within a <a href="https://kubernetes.io/docs/concepts/storage/volumes/" rel="nofollow noreferrer">volume</a>. For example, your <code>CronJob</code> could look like this:</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: &quot;*/1 * * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - /myscript/test.sh
            volumeMounts:
            - name: script-dir
              mountPath: /myscript
          restartPolicy: OnFailure
          volumes:
          - name: script-dir
            hostPath:
              path: /path/to/my/script/dir
              type: Directory
</code></pre>
<p>Example above shows how to use the <a href="https://kubernetes.io/docs/concepts/storage/volumes/#hostpath" rel="nofollow noreferrer">hostPath</a> type of volume in order to mount the script file.</p>
