{"taskGroups":[{"taskSpec":{"runnables":[{"script":{"text":"echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."}}],"maxRunDuration":"255s"},"taskCount":3}],"logsPolicy":{"destination":"CLOUD_LOGGING"}}
{"taskGroups":[{"taskSpec":{"runnables":[{"script":{"text":"echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."},"timeout":"3.5s"}]},"taskCount":3}],"logsPolicy":{"destination":"CLOUD_LOGGING"}}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-02。"],[[["\u003cp\u003eTimeouts can be set for both tasks and runnables to limit their maximum execution time, preventing potential cost overruns and delays.\u003c/p\u003e\n"],["\u003cp\u003eBatch does not set default timeouts for individual tasks and runnables, so if not configured, they can run for up to 14 days before automatic failure.\u003c/p\u003e\n"],["\u003cp\u003eSetting a task timeout limits the total combined run time of all its runnables, while a runnable timeout limits the execution time of that specific runnable.\u003c/p\u003e\n"],["\u003cp\u003eOverlapping timeouts for tasks and runnables will cause a failure when either limit is exceeded, and exceeding a timeout results in exit code \u003ccode\u003e50005\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eTo determine appropriate timeouts, it's recommended to analyze the logs of similar previous jobs to understand the typical run times of tasks and runnables.\u003c/p\u003e\n"]]],[],null,["# Limit run times for tasks and runnables using timeouts\n\nThis document describes how to limit the run times of tasks and runnables by\nsetting timeouts. Alternatively, if you want a runnable to finish as soon as\nevery other runnable in its task has finished running, use a\n[background runnable](/batch/docs/reference/rest/v1/projects.locations.jobs#Runnable.FIELDS.background)\ninstead.\n\nA *timeout* specifies the amount of time that a task or runnable is permitted\nto run.\n[Batch doesn't allow jobs to run for longer than 14 days](/batch/quotas#max-job-duration)\nand doesn't set default timeouts for individual tasks and\nrunnables. Consequently, an individual task or runnable can run for as long as\n14 days before automatic failure. But, if your tasks and runnables aren't\nintended to run for that long, this configuration might cause unexpected costs\nand delays. To prevent excessive run times, you can set timeouts for tasks and\nrunnables.\n\nBefore you begin\n----------------\n\n1. If you haven't used Batch before, review [Get started with Batch](/batch/docs/get-started) and enable Batch by completing the [prerequisites for projects and users](/batch/docs/get-started#prerequisites).\n2.\n\n To get the permissions that\n you need to create a job,\n\n ask your administrator to grant you the\n following IAM roles:\n\n - [Batch Job Editor](/iam/docs/roles-permissions/batch#batch.jobsEditor) (`roles/batch.jobsEditor`) on the project\n - [Service Account User](/iam/docs/roles-permissions/iam#iam.serviceAccountUser) (`roles/iam.serviceAccountUser`) on the [job's service account](/batch/docs/create-run-job-custom-service-account#create-job-service-account), which by default is the [default Compute Engine service account](/compute/docs/access/service-accounts#default_service_account)\n\n\n For more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\n You might also be able to get\n the required permissions through [custom\n roles](/iam/docs/creating-custom-roles) or other [predefined\n roles](/iam/docs/roles-overview#predefined).\n\n\u003cbr /\u003e\n\nSet timeouts\n------------\n\nYou can set timeouts for runnables, tasks, or both. The timeout for a\nrunnable specifies the maximum run time for that runnable.\nThe timeout for a task specifies the maximum run time for that task,\nwhich is the sum of all the individual run times of its runnables.\nFor example, if a task has 3 runnables that all run at the same time for 1\nminute, then the task's run time is 3 minutes, *not* 1 minute.\n\nIf you set overlapping timeouts---such as a timeout for both a runnable and\nthe runnable's task---then only one timeout needs to be exceeded to trigger\nautomatic failure. For example, suppose you set a task's timeout to\n60 seconds and the timeout of each of that task's runnables to\n120 seconds. Then, this example task and all of its runnables fail when the\nsum of the run times of its runnables exceeds 60 seconds, and it's\nimpossible to trigger the 120-second timeouts.\n\nTo choose the appropriate timeout to set for your job's tasks and runnables,\n[analyze the logs](/batch/docs/analyze-job-using-logs) of similar jobs that you\nhave previously run to determine the typical run time for the tasks and\nrunnables for similar workloads.\n| **Important:** Due to a known issue, the logs generated by Batch for exceeded timeout don't indicate whether the task's timeout or the runnable's timeout was exceeded. For a workaround that explains how to identify which timeout was exceeded, see the [known issue](/batch/docs/known-issues#runnable-timeout).\n\n### Set timeout for a task\n\nUse the Google Cloud CLI or REST API to\n[create a job](/batch/docs/create-run-basic-job#create-basic-job) that\nincludes the\n[`maxRunDuration` field](/batch/docs/reference/rest/v1/projects.locations.jobs#taskspec.FIELDS.maxRunDuration)\nin the `taskSpec` object of the JSON file: \n\n {\n \"taskGroups\": [\n {\n \"taskSpec\": {\n ...\n \"maxRunDuration\": \"\u003cvar translate=\"no\"\u003eTIMEOUT\u003c/var\u003e\"\n }\n }\n ]\n }\n\nReplace \u003cvar translate=\"no\"\u003eTIMEOUT\u003c/var\u003e with the maximum number of seconds or\nfractional sections you want to permit the task to run for. For example, `255s`.\n\nA job that sets a 255 second timeout for a task would have a JSON configuration\nfile similar to the following: \n\n {\n \"taskGroups\": [\n {\n \"taskSpec\": {\n \"runnables\": [\n {\n \"script\": {\n \"text\": \"echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks.\"\n }\n }\n ],\n \"maxRunDuration\": \"255s\"\n },\n \"taskCount\": 3\n }\n ],\n \"logsPolicy\": {\n \"destination\": \"CLOUD_LOGGING\"\n }\n }\n\nIf the timeout for a task is exceeded, the task automatically fails and\nthe exceeded timeout is indicated by exit code `50005` in the job's\nstatus events and logs. For more information about exceeded timeouts, see the\n[troubleshooting documentation for exit code 50005](/batch/docs/troubleshooting#task_runs_over_the_maximum_runtime_50005).\n\n### Set timeout for a runnable\n\nUse the Google Cloud CLI or REST API to\n[create a job](/batch/docs/create-run-basic-job#create-basic-job) that\nincludes the\n[`timeout` field](/batch/docs/reference/rest/v1/projects.locations.jobs#runnable.FIELDS.timeout)\nin the `runnable` object of the JSON file: \n\n {\n \"taskGroups\": [\n {\n \"taskSpec\": {\n \"runnables\": [\n {\n ...\n \"timeout\": \"\u003cvar translate=\"no\"\u003eTIMEOUT\u003c/var\u003e\"\n }\n ]\n }\n }\n ]\n }\n\nReplace \u003cvar translate=\"no\"\u003eTIMEOUT\u003c/var\u003e with the maximum number of seconds or\nfractional sections you want to permit the runnable to run for. For example,\n`3.5s`.\n\nA job that sets a 3.5 second timeout for a runnable would have a JSON\nconfiguration file similar to the following: \n\n {\n \"taskGroups\": [\n {\n \"taskSpec\": {\n \"runnables\": [\n {\n \"script\": {\n \"text\": \"echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks.\"\n },\n \"timeout\": \"3.5s\"\n }\n ]\n },\n \"taskCount\": 3\n }\n ],\n \"logsPolicy\": {\n \"destination\": \"CLOUD_LOGGING\"\n }\n }\n\nIf the timeout for a runnable is exceeded, the runnable automatically fails and\nthe exceeded timeout is indicated by exit code `50005` in the job's\nstatus events and logs. For more information about exceeded timeouts, see the\n[troubleshooting documentation for exit code 50005](/batch/docs/troubleshooting#task_runs_over_the_maximum_runtime_50005).\n\nWhat's next\n-----------\n\n- If you have issues creating or running a job, see [Troubleshooting](/batch/docs/troubleshooting).\n- [View jobs and tasks](/batch/docs/view-jobs-tasks).\n- Learn about more [job creation options](/batch/docs/create-run-job#job-creation-options).\n- Learn how to [analyze a job using logs](/batch/docs/analyze-job-using-logs)."]]