Running tasks in your apps
Page last updated:
A task is an app or script, the code of which is included as part of a deployed app, but runs independently in its own container. This topic describes how to run tasks in Cloud Foundry.
Tasks in Cloud Foundry
In contrast to a long-running process (LRP), tasks run for a finite amount of time, then stop. Tasks run in their own containers and are designed to use minimal resources. After a task runs, Cloud Foundry destroys the container running the task.
As a single use object, a task can be checked for its state and for a success or failure message.
Use cases for tasks
Tasks are used to perform one-off jobs, which include:
- Migrating a database
- Sending an email
- Running a batch job
- Running a data processing script
- Processing images
- Optimizing a search index
- Uploading data
- Backing-up data
- Downloading content
How tasks are run
Tasks are always run asynchronously, meaning that they run independently from the parent app or other tasks that run on the same app.
The life cycle of a task is as follows:
A user initiates a task in Cloud Foundry using one of the following mechanisms:
- The
cf run-task APP-NAME "TASK"command. For more information, see Running tasks in your apps. - A Cloud Controller v3 API call. For more information, see the Cloud Foundry API documentation.
- The Cloud Foundry Java Client. For more information, see Cloud Foundry Java Client Library and the Cloud Foundry Java Client repository on GitHub.
- The
Cloud Foundry creates a container specifically for the task.
Cloud Foundry runs the task on the container using the value passed to the
cf run-taskcommand.Cloud Foundry destroys the container.
The container also inherits environment variables, service bindings, and security groups bound to the app.
You cannot SSH into the container running a task.
### Task logging and execution history Any data or messages the task outputs to stdout or stderr is available in the firehose logs of the app. A syslog drain attached to the app receives the task log output. The task execution history is retained for one month. ## Manage tasks At the system level, a user with admin-level privileges can use the Cloud Controller v3 API to view all tasks that are running within an org or space. For more information, see the [Cloud Foundry API documentation](http://v3-apidocs.cloudfoundry.org/version/3.0.0/index.html#list-tasks). Admins can set the default memory, disk usage and log rate quotas for tasks on a global level. Tasks use the same memory, disk usage, and log rate limit defaults as apps, unless you customize them using the `cf run-task` command. For more information about the `cf run-task` command, enter `cf help run-task`.In the BOSH Cloud Controller Worker job, the cc.default_app_memory and cc.default_app_disk_in_mb properties specify the default memory and disk
allocations for tasks.
The cc.default_app_log_rate_limit_in_bytes_per_second property specifies the default log rate limit. For more information about BOSH Cloud Controller Worker jobs, see
BOSH documentation.
To run tasks with the cf CLI, you must install cf CLI v8. To download, install, and uninstall the cf CLI, see Installing the Cloud Foundry Command Line Interface.
Run a task on an app with cf CLI
To run a task on an app with cf CLI v8:
Configure your v3 API manifest with a task as a process type. For more information, see the Cloud Foundry API documentation.
In a terminal, push your app by running:
cf push APP-NAME --taskWhere
APP-NAMEis the name of your app.Run your task on the deployed app by running:
cf run-task APP-NAME --name TASK-NAMEWhere:
APP-NAMEis the name of your app.TASK-NAMEis the name you want to give the task.
cf run-taskallows you to include the--processand--commandflags. Including the--commandflag overrides the manifest property.The following example command runs a task on the
example-appapp:cf run-task example-app --name example-task
When the task runs successfully, you see terminal output similar to the following example:
Creating task for app example-app in org example-org / space development as [email protected]... OK Task 1 has been submitted successfully for execution.
To display the recent logs of the app and all its tasks, run:
cf logs APP-NAME --recentWhere
APP-NAMEis the name of your app.
Get a task for an app
To get a task for a given app:
In a terminal window, run:
cf task APP-NAME TASK-IDWhere
APP-NAMEis the name of your app andTASK-IDis the ID of the task you want to get.The command returns output similar to the following example:
Getting task 4 for app static in org org-1 / space space-1 as admin... id: 4 name: 125c884d state: FAILED start time: 2024-11-22T17:41:19Z command: custom_command memory in mb: 256 disk in mb: 1024 log rate limit: -1 failure reason: APP/TASK/125c884d: Exited with status 127
Tasks can be in the states shown in the following table:
| State | Description |
|---|---|
RUNNING |
The task is in progress. |
FAILED |
The task did not complete. This state occurs when a task does not work correctly or a user cancels the task. |
SUCCEEDED |
The task completed successfully. |
List tasks running on an app
To list the tasks for a given app:
In a terminal window, run:
cf tasks APP-NAMEWhere
APP-NAMEis the name of your app. The command returns output similar to the following example:Getting tasks for app example-app in org example-org / space development as [email protected]... OK
id name state start time command 2 339044ef FAILED Wed, 23 Nov 2016 21:52:52 UTC echo foo; sleep 100; echo bar 1 8d0618cf SUCCEEDED Wed, 23 Nov 2016 21:37:28 UTC bin/rails db:migrate
Tasks can be in these states:
| State | Description |
|---|---|
RUNNING |
The task is in progress. |
FAILED |
The task did not complete. This state occurs when a task does not work correctly or a user cancels the task. |
SUCCEEDED |
The task completed successfully. |
Cancel a task
After you run a task, you might be able to cancel it before it finishes.
To cancel a running task:
In a terminal window, run:
cf terminate-task APP-NAME TASK-IDWhere:
APP-NAMEis the name of your app.TASK-NAMEis the name you want to give the task.
The previous command returns output similar to the following example:
Terminating task 2 of app example-app in org example-org / space development as [email protected]... OK