$ kf run-task test-app --command "printenv" --name foo
Task test-app-6swct is submitted successfully for execution.
$ kf tasks test-app
Listing Tasks in Space: test space
Name ID DisplayName Age Duration Succeeded Reason
test-app-6swct 3 foo 1m 21s True <nil>
管理任务
使用 kf tasks APP_NAME 命令查看应用的所有任务:
$ kf tasks test-app
Listing Tasks in Space: test space
Name ID DisplayName Age Duration Succeeded Reason
test-app-gd8dv 1 test-app-gd8dv 1m 21s True <nil>
取消任务
使用 kf terminate-task 命令取消活动任务:
按任务名称取消任务:
$ kf terminate-task test-app-6w6mz
Task "test-app-6w6mz" is successfully submitted for termination
或者,按 APP_NAME + 任务 ID 取消任务:
$ kf terminate-task test-app 2
Task "test-app-6w6mz" is successfully submitted for termination
已取消的任务的状态为 PipelineRunCancelled。
$ kf tasks test-app
Listing Tasks in Space: test space
Name ID DisplayName Age Duration Succeeded Reason
test-app-gd8dv 1 test-app-gd8dv 1m 21s True <nil>
test-app-6w6mz 2 test-app-6w6mz 38s 11s False PipelineRunCancelled
[[["易于理解","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-04。"],[],[],null,["# Run Tasks\n\nYou can execute short-lived workflows by running them as Tasks in\nKf. Tasks are run under Apps, meaning that each Task must\nhave an associated App. Each Task execution uses the build artifacts from the\nparent App. Because Tasks are short-lived, the App is not deployed as a\nlong-running application, and no routes should be created for the App or the Task.\n\nPush an App for running Tasks\n-----------------------------\n\n1. Clone the [test-app repo](https://github.com/cloudfoundry-samples/test-app) repo:\n\n git clone https://github.com/cloudfoundry-samples/test-app test-app\n cd test-app\n\n2. Push the App.\n\n Push the App with the `kf push APP_NAME --task` command. The `--task` flag\n indicates that the App is meant to be used for running Tasks, and thus no\n routes are created on the App, and it is not deployed as a long-running\n application: \n\n kf push test-app --task\n\n3. Confirm that no App instances or routes were created by listing the App:\n\n kf apps\n\n Notice that the App is not started and has no URLs: \n\n Listing Apps in Space: test-space\n Name Instances Memory Disk CPU URLs\n test-app stopped 1Gi 1Gi 100m \u003cnil\u003e\n\nRun a Task on the App\n---------------------\n\nWhen you run a Task on the App, you can optionally specify a start command by\nusing the `--command` flag. If no start command is specified, it uses the start\ncommand specified on the App. If the App doesn't have a start command specified,\nit looks up the CMD configuration of the container image. A start command must\nexist in order to run the Task successfully. \n\n kf run-task test-app --command \"printenv\"\n\nYou see something similar to this, confirming that the Task was submitted: \n\n Task test-app-gd8dv is submitted successfully for execution.\n\nThe Task name is automatically generated, prefixed with the App name, and\nsuffixed with an arbitrary string. The Task name is a unique identifier for\nTasks within the same cluster.\n\nSpecify Task resource limits\n----------------------------\n\nResource limits (such as CPU cores/Memory limit/Disk quota) can be specified in\nthe App (during `kf push`) or during the `kf run-task` command. The limits\nspecified in the `kf run-task` command take prededence over the limits specified\non the App.\n\nTo specify resource limits in an App, you can use the `--cpu-cores`,\n`--memory-limit`, and `--disk-quota` flags in the `kf push` command: \n\n kf push test-app --command \"printenv\" --cpu-cores=0.5 --memory-limit=2G --disk-quota=5G --task\n\nTo override these limits in the App, you can use the `--cpu-cores`,\n`--memory-limit`, and `--disk-quota` flags in the `kf run-task` command: \n\n kf run-task test-app --command \"printenv\" --cpu-cores=0.5 --memory-limit=2G --disk-quota=5G\n\nSpecify a custom display name for a Task\n----------------------------------------\n\nYou can optionally use the `--name` flag to specify a custom display name for a\nTask for easier identification/grouping: \n\n $ kf run-task test-app --command \"printenv\" --name foo\n Task test-app-6swct is submitted successfully for execution.\n\n $ kf tasks test-app\n Listing Tasks in Space: test space\n Name ID DisplayName Age Duration Succeeded Reason\n test-app-6swct 3 foo 1m 21s True \u003cnil\u003e\n\nManage Tasks\n------------\n\nView all Tasks of an App with the `kf tasks APP_NAME` command: \n\n $ kf tasks test-app\n Listing Tasks in Space: test space\n Name ID DisplayName Age Duration Succeeded Reason\n test-app-gd8dv 1 test-app-gd8dv 1m 21s True \u003cnil\u003e\n\nCancel a Task\n-------------\n\nCancel an active Task by using the `kf terminate-task` command:\n\n- Cancel a Task by Task name:\n\n $ kf terminate-task test-app-6w6mz\n Task \"test-app-6w6mz\" is successfully submitted for termination\n\n- Or cancel a Task by \u003cvar translate=\"no\"\u003eAPP_NAME\u003c/var\u003e + Task ID:\n\n $ kf terminate-task test-app 2\n Task \"test-app-6w6mz\" is successfully submitted for termination\n\n| **Note:** You can only cancel Tasks that are pending/running. Completed Tasks are not cancellable.\n\nCancelled Tasks have `PipelineRunCancelled` status. \n\n $ kf tasks test-app\n Listing Tasks in Space: test space\n Name ID DisplayName Age Duration Succeeded Reason\n test-app-gd8dv 1 test-app-gd8dv 1m 21s True \u003cnil\u003e\n test-app-6w6mz 2 test-app-6w6mz 38s 11s False PipelineRunCancelled\n\nView Task logs\n--------------\n\nView logs of a Task by using the `kf logs APP_NAME --task` command: \n\n $ kf logs test-app --task"]]