Create and run a basic job

This document explains the basics for Batch job creation: how to create and run a job that is based on a script or container image and use predefined and custom variables. To learn more about creating and running jobs, see Job creation and execution overview.

Before you begin

  1. If you haven't used Batch before, review Get started with Batch and enable Batch by completing the prerequisites for projects and users.
  2. To get the permissions that you need to create a job, ask your administrator to grant you the following IAM roles:

    For more information about granting roles, see Manage access to projects, folders, and organizations.

    You might also be able to get the required permissions through custom roles or other predefined roles.

  3. Each time you create a job, make sure the job has a valid resource configuration.
    • Unless the default values are sufficient, you must specify the compute resources per task (computeResource) fields required for each task to run, as shown in this document.
    • If you specify any VM resources for a job (any instances[].policy, instances[].instanceTemplate, or instanceFlexibilityPolicy field), you must make sure that these fields are compatible with each other and the job.
    For more information about resource configuration for jobs, see Job resources.
  4. Each time you create a job, make sure the job has a valid network configuration.
    • If you don't have any specific networking requirements for your workload or project and you haven't modified the default network for your project, no action is required.
    • Otherwise, you need to configure networking when you create a job. Learn how to configure networking for a job before you create a basic job, so you can modify the examples below to meet your networking requirements.
    For more information about the network configuration for a job, see Batch networking overview.
  5. Each time you create a job, make sure the job has a valid VM operating system (OS) environment.
    • If you don't have any specific VM OS image or boot disk requirements for your workload or project, no action is required.
    • Otherwise, you need to prepare a valid VM OS environment option. Before creating a basic job, either allow the default configuration for the VM OS environment or learn how to customize the VM OS environment so you can modify the examples below to meet your requirements.
    For more information about the VM OS environment for a job, see VM OS environment overview.

Create a basic job

For information about all the fields you can specify for a job, see the reference documentation for the projects.locations.jobs REST resource. To summarize, a job consists of an array of one or more tasks that all run one or more runnables, which are the executable script(s) and/or container(s) for your job. To cover the basics, this section explains how to create an example job with just one runnable, either a script or a container image:

  • If you want to use Batch to write jobs that run a container image, see create a container job.
  • Otherwise, if you aren't sure if you want to use container images or if you are unfamiliar with containers, creating a script job is recommended.

The example job for both types of jobs contains a task group with an array of 4 tasks. Each task prints a message and its index to the standard output and Cloud Logging. The definition for this job specifies a parallelism of 2, which indicates that the job should run on 2 VMs to allow 2 tasks to run at a time.

Create a basic container job

You can select or create a container image to provide the code and dependencies for your job to run from any compute environment. For more information, see Working with container images and Running containers on VM instances.

You can create a basic container job using the Google Cloud console, gcloud CLI, Batch API, Go, Java, Node.js, Python, or C++.

Console

To create a basic container job using the Google Cloud console, do the following:

  1. In the Google Cloud console, go to the Job list page.

    Go to Job list

  2. Click Create. The Create batch job page opens. In the left pane, the Job details page is selected.

  3. Configure the Job details page:

    1. Optional: In the Job name field, customize the job name.

      For example, enter example-basic-job.

    2. Configure the Task details section:

      1. In the New runnable window, add at least one script or container for this job to run.

        For example, to add one container, do the following:

        1. Select Container image URL (default).

        2. In the Container image URL field, enter the URL for a container image that you want to run for each task in this job.

          For example, to use the busybox Docker container image, enter the following URL:

          gcr.io/google-containers/busybox
          
        3. Optional: To override the container image's ENTRYPOINT command, enter a command in the Entry point field.

          For example, enter the following:

          /bin/sh
          
        4. Optional: To override the container image's CMD command, do the following:

          1. Select the Override container image's CMD command checkbox. A field appears.

          2. In the field, enter one or more commands, separating each command with a new line.

            For example, enter the following commands:

            -c
            echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks.
            
          3. Click Done.

      2. In the Task count field, enter the number of tasks for this job. The value must be a whole number between 1 and the tasks per task group limit.

        For example, enter 4.

      3. In the Parallelism field, enter the number of tasks to run concurrently. The number cannot be larger than the total number of tasks and must be a whole number between 1 and the parallel tasks per job limit.

        For example, enter 2.

  4. Configure the Resource specifications page:

    1. In the left pane, click Resource specifications. The Resource specifications page opens.

    2. In the VM provisioning model section, select one of the following options for the provisioning model for this job's VMs:

      • If your job can withstand preemption and you want discounted VMs, select Spot. Learn more about recommendations for Spot VMs.

      • Otherwise, select Standard.

      For example, select Standard (default).

    3. Select the location for this job:

      1. In the Region field, select a region.

        For example, select us-central1 (Iowa) (default).

      2. In the Zone field, do one of the following:

        • If you want to restrict this job to run in a specific zone only, select a zone.

        • Otherwise, select any.

        For example, select any (default).

    4. Select one of the following machine families:

      • For common workloads, click General purpose.

      • For performance-intensive workloads, click Compute optimized.

      • For memory-intensive workloads, click Memory optimized.

      • For accelerator-optimized workloads, click GPUs. For more information, see Create and run a job that uses GPUs.

      For example, click General purpose (default).

    5. In the Series field, select a machine series for this job's VMs.

      For example, if you selected General purpose for the machine family, select E2 (default).

    6. In the Machine type field, select a machine type for this job's VMs.

      For example, if you selected E2 for the machine series, select e2-medium (2 vCPU, 4 GB memory) (default).

    7. Configure the amount of VM resources required for each task:

      1. In the Cores field, enter the amount of vCPUs per task.

        For example, enter 1 (default).

      2. In the Memory field, enter the amount of RAM in GB per task.

        For example, enter 0.5 (default).

  5. Optional: To review the job configuration, in the left pane, click Preview.

  6. Click Create.

The Job details page displays the job that you created.

gcloud

To create a basic container job using the gcloud CLI, do the following:

  1. Create a JSON file that specifies your job's configuration details. For example, to create a basic container job, create a JSON file with the following contents. For more information about all the fields you can specify for a job, see the reference documentation for the projects.locations.jobs REST resource.

    {
        "taskGroups": [
            {
                "taskSpec":