7

I am trying to create a cronjob that runs the command date in a single busybox container. The command should run every minute and must complete within 17 seconds or be terminated by Kubernetes. The cronjob name and container name should both be hello.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  jobTemplate:
    metadata:
      name: hello
    spec:
      completions: 1
      activeDeadlineSeconds: 17
      template:
        metadata:
          creationTimestamp: null
        spec:
          containers:
          - image: busybox
            name: hello
            command: ["/bin/sh","-c","date"]
            resources: {}
          restartPolicy: OnFailure
  schedule: '*/1 * * * *'
status: {}

I want to verify that the job executed successfully at least once. I tried it using the command k get cronjob -w which gives me this result.

enter image description here

Is there another way to verify that the job executes successfully? Is it a good way to add a command date to the container?

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
O.Man
  • 585
  • 2
  • 9
  • 20

3 Answers3

12

CronJob internally creates Job which internally creates Pod. Watch for the job that gets created by the CronJob

kubectl get jobs --watch

The output is similar to this:

NAME               COMPLETIONS   DURATION   AGE
hello-4111706356   0/1                      0s
hello-4111706356   0/1           0s         0s
hello-4111706356   1/1           5s         5s

And you can see the number of COMPLETIONS

#Replace "hello-4111706356" with the job name in your system

pods=$(kubectl get pods --selector=job-name=hello-4111706356 --output=jsonpath={.items[*].metadata.name})

Check the pod logs

kubectl logs $pods
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
  • The strange thing is that the pod has status RunContainerError. – O.Man Jul 10 '20 at 14:19
  • then it did not run properly..check by describing the pod and logs of the pod – Arghya Sadhu Jul 10 '20 at 14:20
  • I checked the logs and see this message => Error: failed to start container "hello": Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"/bins/sh\": stat /bins/sh: nosuch file or directory": unknown – O.Man Jul 10 '20 at 14:26
  • I tried it in browser where it obviously does not work but in my terminal it works good. Thank you for help. – O.Man Jul 10 '20 at 14:36
  • sure..if you face any further issue I suggest to ask a new question with error logs – Arghya Sadhu Jul 10 '20 at 14:45
2

you can check the logs of the pods created by the cronjob resource. Have a look at this question and let me know if this solves your query.

codeaprendiz
  • 2,703
  • 1
  • 25
  • 49
1

You can directly check the status of the jobs. Cronjob is just controlling a Kubernetes job.

Run kubectl get jobs and it will give you the completion status.

> kubectl get jobs
NAME           COMPLETIONS   DURATION   AGE
datee-job      0/1 of 3      24m        24m

François
  • 1,793
  • 14
  • 19