How to trigger a cronjob manually in Kubernetes Cluster


In this article, we will see how to trigger a cronjob manually in Kubernetes Cluster. Cronjob in kubernetes is used for scheduling job to perform certain task such as backups, report generation, migration, data sync etc. Cronjob object is like a one line of crontab file used in Linux/Unix systems. A job can create one or more pods to complete the task. It will continue to retry execution of the pods until the specified number of them successfully complete and terminate. After reaching the specified number of successful completions, job will be marked as completed.

Sometimes, it is possible that after creating a cronjob, you would like to run it immediately for various reasons such as for testing the functionality, taking latest backup, urgent requirement of generating a report etc. Here we will see how you can easily do that. But even before that, let's understand the crontab syntax  first.

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of the month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
│ │ │ │ │                                   7 is also Sunday on some systems)
│ │ │ │ │                                   OR sun, mon, tue, wed, thu, fri, sat
│ │ │ │ │
* * * * *


How to trigger a cronjob manually in Kubernetes Cluster

How to trigger a cronjob manually in Kubernetes Cluster

Also Read: Kubernetes pod stuck in terminating state for long

When it comes to triggering a cronjob manually, you can use kubectl utility to immediately create and trigger the job. The syntax to use is kubectl create job --from=cronjob/<name_of_cronjob> <name_of job_to_run>. For example, in our case to trigger a cronjob called fscm-portal-api, we have to use kubectl create job --from=cronjob/fscm-portal-api fscm-portal-api-manual command as shown below. This will immediately create and run the fscm-portal-api-manual job.

[cyberithub@node1 ~ % kubectl create job --from=cronjob/fscm-portal-api fscm-portal-api-manual

You can check the list of running or completed jobs by using kubectl get jobs command as shown below.

[cyberithub@node1 ~ % kubectl get jobs
NAME                         COMPLETIONS       DURATION   AGE
fscm-portal-api-manual       0/1               7m35s      9m
hcm-portal-api-29849074      1/1               2m6s       6h

The output can be interpreted as follows:-

  • NAME is the name of the job triggered from cronjob
  • COMPLETIONS shows the total number of pod completed/total number of pods in job
  • DURATION is the total amount of time for which the job is running
  • AGE is the total elapsed since the pod is created

To check the current status of the job, you can run kubectl get job fscm-portal-api-manual -o yaml command and check the status section. If the job is still running, it will show in active and running state as shown below.

[cyberithub@node1 ~ % kubectl get job fscm-portal-api-manual -o yaml
  active: 1
  ready: 1
  startTime: "2023-08-04T11:03:32Z"

To get the name of the pod job has created, you need to look for field from kubectl get job fscm-portal-api-manual -o yaml output as shown below.

[cyberithub@node1 ~ % kubectl get job fscm-portal-api-manual -o yaml
  name: fscm-portal-api-manual-008

You can also check the logs of the running job by checking the logs of the pod it created using kubectl logs -f <pod_name> command. Since in our case, the pod that fscm-portal-api-manual job has created is fscm-portal-api-manual-008-vwh75 so to check the logs, we have to run kubectl logs -f fscm-portal-api-manual-008-vwh75 command as shown below.

[cyberithub@node1 ~ % kubectl logs -f fscm-portal-api-manual-008-vwh75

If the job is completed, then it will show the status as complete and succeeded as you can see below.

[cyberithub@node1 ~ % kubectl get job fscm-portal-api-manual -o yaml
  completionTime: "2023-08-04T11:31:41Z"
  - lastProbeTime: "2023-08-04T11:31:41Z" 
    lastTransitionTime: "2023-08-04T11:31:41Z"
    status: "True"
    type: Complete
  ready: 0
  startTime: "2023-08-04T11:03:32Z"
  succeeded: 1

Once you are done using a cronjob, you can remove it by running kubectl delete cronjob <cronjob_name> command. This will clean up all the pods it created to complete the task.  If you suspend the job then it will not start as scheduled. If the job is already running then it will delete all its pods until the job is resumed again.

[cyberithub@node1 ~ % kubectl delete cronjob fscm-portal-api
cronjob.batch "fscm-portal-api" deleted

Leave a Comment