# CronJobs

A [Kubernetes ](https://kubernetes.io/)CronJob is a variant of a [Kubernetes Job](https://docs.duplocloud.com/docs/automation-platform/kubernetes-overview/jobs) you can schedule to run at periodic intervals.

See the Kubernetes [CronJob](https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/) documentation for more information.

## Creating a Kubernetes CronJob in the DuploCloud portal

1. In the DuploCloud Portal, navigate to **Kubernetes** -> **CronJob**.
2. Click **Add**. The **Add Kubernetes CronJob** page displays.
3. In the **Basic Options** step, specify the Kubernetes CronJob name.
4. In the **Schedule** field, specify the Cron Schedule in Cron Format. Click the Info Tip icon for examples. When specifying a **Schedule** in Cron Format, ensure you separate each value with a space. For example, `0 0 * * 0` is a valid Cron Format input; `00**0` is not. See the [Kubernetes documentation](https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/#writing-a-cronjob-spec) for detailed information about Cron Format.
5. In the **Container - 1** area, specify the **Container Name** and associated **Docker Image**.

<figure><img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2F9Lh3ybgynqCYimDBmaUX%2Fcron%201.png?alt=media&#x26;token=de7dbe87-94bf-45d6-b1a1-50ca304984ff" alt=""><figcaption><p>The <strong>Add Kubernetes CronJob</strong> page with <strong>Container Nam</strong>e and <strong>Docker image</strong> fields filled.</p></figcaption></figure>

6. In the **Command** field, specify the command attributes for **Container - 1**. Click the Info Tip icon for examples. Select and copy commands as needed.

<figure><img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2FbSAb0Dacg7JrqEJjVtLQ%2Fcrom3.png?alt=media&#x26;token=907c4cec-0590-4827-a1d1-65cfcd93e68a" alt=""><figcaption><p>The <strong>Add Kubernetes CronJob</strong> page with the <strong>Command</strong> field for <strong>Container - 1</strong> filled<strong>.</strong></p></figcaption></figure>

7. To run the Kubernetes CronJob to completion, you must specify a Kubernetes [Init Container](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/).  Click the **Add Container** <img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2F9254oS0Y7djkb6XxVCTW%2Fchevron_Down_arrow.png?alt=media&#x26;token=0dc842d7-2dbb-42b4-8f06-26f1e53720b7" alt="" data-size="line"> button and select the **Add Init Container** option. The **Init Container - 1** area displays.

<figure><img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2FJCdMQIG8KY7CwrhAVpJb%2Fcron4.png?alt=media&#x26;token=91ee6012-1669-4a5f-9a7d-2e0af9fdb4be" alt=""><figcaption><p><strong>Add Init Container</strong> area.</p></figcaption></figure>

8. In the **Init Container - 1** area, specify the **Container Name** and associated **Docker Image**.
9. Click **Next** to open the **Advanced Configuration** step.
10. In the **Other Spec Configuration** field, specify the Kubernetes CronJob spec (in YAML) for **Init Container - 1**. Click the Info Tip icon ( <img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2Fj32iLFAr0qiapahv3QJk%2Finfo_tip_black.png?alt=media&#x26;token=374f5522-02cd-4001-932b-762fde447edc" alt="" data-size="line"> ) for examples. Select and copy commands as needed

<figure><img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2FD0FFnhj34hrvMELYxCMn%2Fcron6.png?alt=media&#x26;token=1c4590ab-0a64-4dc2-a22e-90cf7bec2f68" alt=""><figcaption><p>The <strong>Other Spec Configuration</strong> window on the <strong>Add Kubernetes CronJob, Advanced Configuration</strong> page<strong>.</strong></p></figcaption></figure>

<figure><img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2FdFRPn4xE7H8BnpbAjozF%2Fcron7.png?alt=media&#x26;token=fbf11265-5cc3-407f-aa39-e5528999c411" alt=""><figcaption><p>The <strong>Add Kubernetes CronJob</strong> page with the <strong>Other Spec Configuration</strong> field completed<strong>.</strong></p></figcaption></figure>

11. Click **Create**. The Kubernetes CronJob is created and displayed on the **CronJob** page. It will run according to the schedule you specified.&#x20;

<figure><img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2FQfUgaiILbkm18ZyOwJOE%2Fcron%20final.png?alt=media&#x26;token=7f03d6d2-c0e4-46bf-97b4-06096fae8507" alt=""><figcaption><p><strong>K8s CronJob</strong> tab displaying Kubernetes Job <strong>CALCULATEPI.</strong></p></figcaption></figure>

&#x20;

## Managing Kubernetes CronJobs faults

You can manage/override Kubernetes Jobs faults on a Tenant or CronJob level. If a CronJob fails, and no Tenant- or Job-level fault setting is configured, DuploCloud will generate a fault by default.&#x20;

### **Tenant-level Kubernetes CronJobs faults**

Enable or disable faults for failed Kubernetes CronJobs in a specific Tenant.

1. From the DuploCloud Portal, navigate to **Administrator** -> **Tenant.**
2. Click the Tenant name in the **NAME** column.&#x20;
3. Select the **Settings** tab, and click **Add**. The **Add Tenant Feature** pane displays.&#x20;
4. From the **Select Feature** list box, select **Enable K8s job fault logging by default**, and use the toggle switch to enable or disable the setting.&#x20;
5. Click **Add**. The CronJobs fault setting is added. \\

   <div align="left"><figure><img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2FlxYfLVRjrRVryqz5zLVH%2Ftenant%20faults%20feature.png?alt=media&#x26;token=f5965c17-7e32-4228-a8e2-f4a580276adb" alt=""><figcaption><p>The <strong>Add Tenant Feature</strong> pane with <strong>Enable K8s Job fault logging by default</strong> enabled</p></figcaption></figure></div>

You can view the CronJobs fault setting on the **Tenants** page (Navigate to **Administrator** -> **Tenant**, select the Tenant name) under the **Settings** tab. If the value is **true**, DuploCloud will generate a fault. If the value is **false**, DuploCloud will not generate a fault.&#x20;

<figure><img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2FcHvAWxEHeHQfxD4HM9nQ%2Ftenant%20setting%20set.png?alt=media&#x26;token=76bb2ae9-b4b9-4afa-9562-d27c88be7c46" alt=""><figcaption><p>The <strong>Tenant</strong> details page, <strong>Setting</strong>s tab, showing the configured Jobs fault setting</p></figcaption></figure>

### **Jobs-level Kubernetes CronJobs faults**

You can configure the faults for a specific CronJob when creating the CronJob in DuploCloud. Fault settings added this way override Tenant-level settings. On the **Add Kubernetes Job** page, in the **Metadata Annotations** field, enter:&#x20;

`duplocloud.net/fault/when-failed: true.` or&#x20;

`duplocloud.net/fault/when-failed: false.`

When the value is true and the CronJob fails, DuploCloud will generate a fault. When the value is false and the CronJob fails, a fault will not be generated.&#x20;

## Viewing a Kubernetes CronJob&#x20;

1. In the DuploCloud Portal, navigate to **Kubernetes** -> **CronJobs**.
2. Select the Kubernetes CronJob you want to view and click the **Overview, Schedule**, and **Details** tabs for more information about the CronJob schedule and history.&#x20;

You can also view details of a Kubernetes CronJob by clicking on the **menu icon** ( <img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2FgZZ67PeXb7LCgiJvqmc5%2FKabab_three_Vertical_dots.png?alt=media&#x26;token=db84036f-dda9-4740-a2e2-726bfd7cac95" alt="" data-size="line"> ) icon to the left of the job name and selecting **View**.

<figure><img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2F8i5jUFltlLY3wMsiBCUO%2Fcron9.png?alt=media&#x26;token=045c202a-ca23-4f65-9cfb-62d5f38a2c38" alt=""><figcaption><p><strong>Overview</strong> tab for Kubernetes Job <strong>MYCRONJOB.</strong></p></figcaption></figure>

<figure><img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2FQ7dZkkT14npJwU6CCZBw%2Fcronview.png?alt=media&#x26;token=561c70c5-f80b-4822-86ec-4c2252e3c259" alt=""><figcaption><p>CronJob options menu with <strong>View</strong> option highlighted.</p></figcaption></figure>

### Using the Container page to view linked Kubernetes CronJobs

You can view Kubernetes CronJobs linked to containers by clicking the container name on the **Containers** page (**Kubernetes** -> **Containers**). \\

<div align="left"><figure><img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2FMzIHL90ekkYpE8yOkKj1%2Fj29.png?alt=media&#x26;token=b07f612f-e0f7-4238-95a5-4fcbefbc2c48" alt=""><figcaption><p>Clicking the Container <strong>Name</strong> on the <strong>Containers</strong> page to view a linked K8s CronJob</p></figcaption></figure></div>

You can filter container names by using the search field at the top of the page, as in this example:

<figure><img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2FqXaD6i0LsMmbjuWbtSUq%2Fsearch%20containers.png?alt=media&#x26;token=437f2fa4-4650-4846-886d-7e821a0ae6cf" alt=""><figcaption><p>Highlighted search field on the <strong>Containers</strong> page </p></figcaption></figure>

## Editing a Kubernetes CronJob

1. In the DuploCloud Portal, navigate to **Kubernetes** -> **CronJob**.
2. Select the Kubernetes CronJob you want to edit.&#x20;
3. Click the **options menu** ( <img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2FgZZ67PeXb7LCgiJvqmc5%2FKabab_three_Vertical_dots.png?alt=media&#x26;token=db84036f-dda9-4740-a2e2-726bfd7cac95" alt="" data-size="line"> ) icon to the left of the Kubernetes CronJob name and select **Edit**.

You can edit and modify the following fields in the DuploCloud Portal:

* **Cleanup After Finished in Seconds**
* **Other Spec Configuration**
* **Metadata Annotations**
* **Labels**

<figure><img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2FLTu3wgpW0TeG42XGo3gE%2Fcron%20edit.png?alt=media&#x26;token=bb2975f4-cde1-42c3-b827-34cf0f482e9d" alt=""><figcaption><p>CronJob options menu with <strong>Edit</strong> option highlighted</p></figcaption></figure>

## Deleting a Kubernetes CronJob

1. In the DuploCloud Portal, navigate to **Kubernetes** -> **CronJob.**
2. Select the Kubernetes CronJob you want to delete.&#x20;
3. Click the Options Menu (<img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2FgZZ67PeXb7LCgiJvqmc5%2FKabab_three_Vertical_dots.png?alt=media&#x26;token=db84036f-dda9-4740-a2e2-726bfd7cac95" alt="" data-size="line">) icon to the left of the Kubernetes CronJob name and select **Delete**.

<figure><img src="https://2471407984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F68cb0s9ce5UIUKWPuYs8%2Fuploads%2F6YNQCkeO73bwz8RObNHO%2Fcron%20delete.png?alt=media&#x26;token=29325f1e-7d69-4a59-bb78-79a575e27401" alt=""><figcaption><p>CronJob options menu with <strong>Delete</strong> option highlighted</p></figcaption></figure>

## Running CronJobs on Shared Hosts

DuploCloud supports running Kubernetes CronJobs on Shared Hosts in both AWS and Azure.&#x20;

For detailed steps, see the [Shared Hosts documentation for AWS](https://docs.duplocloud.com/docs/automation-platform/overview/use-cases/hosts-vms/adding-shared-hosts) and [Shared Hosts documentation for Azure](https://docs.duplocloud.com/docs/automation-platform/overview-2/use-cases/hosts-vms/shared-hosts).
