Run background tasks with WebJobs in Azure App Service
Deploy WebJobs by using the Azure portal to upload an executable or script. You can run background tasks in the Azure App Service.
If instead of the Azure App Service you are using Visual Studio 2019 to develop and deploy WebJobs, see Deploy WebJobs using Visual Studio.
Overview
WebJobs is a feature of Azure App Service that enables you to run a program or script in the same instance as a web app, API app, or mobile app. There is no additional cost to use WebJobs.
You can use the Azure WebJobs SDK with WebJobs to simplify many programming tasks. WebJobs is not yet supported for App Service on Linux. For more information, see What is the WebJobs SDK.
Azure Functions provides another way to run programs and scripts. For a comparison between WebJobs and Functions, see Choose between Flow, Logic Apps, Functions, and WebJobs.
WebJob types
The following table describes the differences between continuous and triggered WebJobs.
Continuous | Triggered |
---|---|
Starts immediately when the WebJob is created. To keep the job from ending, the program or script typically does its work inside an endless loop. If the job does end, you can restart it. Typically used with WebJobs SDK. | Starts only when triggered manually or on a schedule. |
Runs on all instances that the web app runs on. You can optionally restrict the WebJob to a single instance. | Runs on a single instance that Azure selects for load balancing. |
Supports remote debugging. | Doesn't support remote debugging. |
Code is deployed under \site\wwwroot\app_data\Jobs\Continuous . |
Code is deployed under \site\wwwroot\app_data\Jobs\Triggered . |
Note
A web app can time out after 20 minutes of inactivity, and only requests to the actual web app can reset the timer. Viewing the app's configuration in the Azure portal or making requests to the advanced tools site (https://<app_name>.scm.azurewebsites.net
) doesn't reset the timer. If you set the web app that hosts your job to run continuously, run on a schedule, or use event-driven triggers, enable the Always on setting on your web app's Azure Configuration page. The Always on setting helps to make sure that these kinds of WebJobs run reliably. This feature is available only in the Basic, Standard, and Premium pricing tiers.
Supported file types for scripts or programs
The following file types are supported:
- .cmd, .bat, .exe (using Windows cmd)
- .ps1 (using PowerShell)
- .sh (using Bash)
- .php (using PHP)
- .py (using Python)
- .js (using Node.js)
- .jar (using Java)
Create a continuous WebJob
Important
When you have source control configured for your application, Webjobs should be deployed as part of the source control integration. After source control is configured for your application, a WebJob can't be added from the Azure portal.
In the Azure portal, go to the App Service page of your App Service web app, API app, or mobile app.
In the left pane of your app's App Service page, search for and select WebJobs.
On the WebJobs page, select Add.
Fill in the Add WebJob settings as specified in the table.
Setting Sample value Description Name myContinuousWebJob A name that is unique within an App Service app. Must start with a letter or a number and cannot contain special characters other than "-" and "_". File Upload ConsoleApp.zip A .zip file that contains your executable or script file as well as any supporting files needed to run the program or script. The supported executable or script file types are listed in the Supported file types section. Type Continuous The WebJob types are described earlier in this article. Scale Multi instance Available only for Continuous WebJobs. Determines whether the program or script runs on all instances or just one instance. The option to run on multiple instances doesn't apply to the Free or Shared pricing tiers. Select OK.
The new WebJob appears on the WebJobs page. If you see a message that says the WebJob was added, but you don't see it, select Refresh.
To stop or restart a continuous WebJob, right-click the WebJob in the list and select Stop or Start.
Create a manually triggered WebJob
In the Azure portal, search for and select App Services.
Select your web app, API app, or mobile app from the list.
In the left pane of your app's App Service page, select WebJobs.
On the WebJobs page, select Add.
Fill in the Add WebJob settings as specified in the table.
Setting Sample value Description Name myTriggeredWebJob A name that is unique within an App Service app. Must start with a letter or a number and cannot contain special characters other than "-" and "_". File Upload ConsoleApp.zip A .zip file that contains your executable or script file as well as any supporting files needed to run the program or script. The supported executable or script file types are listed in the Supported file types section. Type Triggered The WebJob types are described previously in this article. Triggers Manual Select OK.
The new WebJob appears on the WebJobs page. If you see a message that says the WebJob was added, but you don't see it, select Refresh.
To run the WebJob, right-click its name in the list and select Run.
Create a scheduled WebJob
A scheduled Webjob is also triggered. You can schedule the trigger to occur automatically on the schedule you specify.
In the Azure portal, search for and select App Services.
Select your web app, API app, or mobile app from the list.
In the left pane of your app's App Service page, select WebJobs.
On the WebJobs page, select Add.
Fill in the Add WebJob settings as specified in the table.
Setting Sample value Description Name myScheduledWebJob A name that is unique within an App Service app. Must start with a letter or a number and cannot contain special characters other than "-" and "_". File Upload ConsoleApp.zip A .zip file that contains your executable or script file as well as any supporting files needed to run the program or script. The supported executable or script file types are listed in the Supported file types section. Type Triggered The WebJob types are described earlier in this article. Triggers Scheduled For the scheduling to work reliably, enable the Always On feature. Always On is available only in the Basic, Standard, and Premium pricing tiers. CRON Expression 0 0/20 * * * * CRON expressions are described in the following section. Select OK.
The new WebJob appears on the WebJobs page. If you see a message that says the WebJob was added, but you don't see it, select Refresh.
NCRONTAB expressions
You can enter a NCRONTAB expression in the portal or include a settings.job
file at the root of your WebJob .zip file, as in the following example:
{
"schedule": "0 */15 * * * *"
}
To learn more, see Scheduling a triggered WebJob.
Note
The default time zone used to run CRON expressions is Coordinated Universal Time (UTC). To have your CRON expression run based on another time zone, create an app setting for your function app named WEBSITE_TIME_ZONE. To learn more, see NCRONTAB time zones.
Manage WebJobs
You can manage the running state individual WebJobs running in your site in the Azure portal. Just go to Settings > WebJobs, choose the WebJob, and you can start and stop the WebJob. You can also view and modify the password of the webhook that runs the WebJob.
You can also add an application setting named WEBJOBS_STOPPED
with a value of 1
to stop all WebJobs running on your site. This can be handy as a way to prevent conflicting WebJobs from running both in staging and production slots. You can similarly use a value of 1
for the WEBJOBS_DISABLE_SCHEDULE
setting to disable triggered WebJobs in the site or a staging slot. For slots, remember to enable the Deployment slot setting option so that the setting itself doesn't get swapped.
View the job history
Select the WebJob and then to see the history, select Logs.
In the WebJob Details page, select a time to see details for one run.
In the WebJob Run Details page, select Toggle Output to see the text of the log contents.
To see the output text in a separate browser window, select download. To download the text itself, right-click download and use your browser options to save the file contents.
Select the WebJobs breadcrumb link at the top of the page to go to a list of WebJobs.
Next steps
The Azure WebJobs SDK can be used with WebJobs to simplify many programming tasks. For more information, see What is the WebJobs SDK.
Feedback
Submit and view feedback for