Uredi

Deli z drugimi prek


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're using Visual Studio to develop and deploy WebJobs, see Deploy WebJobs using Visual Studio.

Note

WebJobs for Windows container, Linux code, and Linux container is in preview. WebJobs for Windows code is generally available and not in preview.

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. All app service plans support WebJobs. There's no extra cost to use WebJobs.

Note

Starting June 1, 2024, all newly created App Service apps will have the option to generate a unique default hostname using the naming convention <app-name>-<random-hash>.<region>.azurewebsites.net. Existing app names will remain unchanged.

Example: myapp-ds27dh7271aah175.westus-01.azurewebsites.net

For further details, refer to Unique Default Hostname for App Service Resource.

You can use the Azure WebJobs SDK with WebJobs to simplify many programming tasks. 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

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)
.js (using Node.js)
.jar (using Java)

The necessary runtimes to run these file types are already installed on the web app instance.

Continuous vs. triggered WebJobs

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.

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.

  1. In the Azure portal, go to the App Service page of your App Service web app, API app, or mobile app.

  2. From the left pane, select WebJobs, then select Add.

    Screenshot that shows how to add a WebJob in an App Service app in the portal.

  3. Fill in the Add WebJob settings as specified in the table, then select Create Webjob.

    Screenshot that shows how to configure a mult-instance continuous WebJob for an App Service app.

    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 must not contain special characters other than "-" and "_".
    File Upload ConsoleApp.zip A .zip file that contains your executable or script file and 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 one instance. The option to run on multiple instances doesn't apply to the Free or Shared pricing tiers.
  4. 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.

  5. To stop or restart a continuous WebJob, right-click the WebJob in the list and select the Stop or Run button, then confirm your selection.

    Screenshot that shows how to stop a continuous WebJob in the Azure portal.

Create a manually triggered WebJob

  1. In the Azure portal, go to the App Service page of your App Service web app, API app, or mobile app.

  2. From the left pane, select WebJobs, then select Add.

    Screenshot that shows how to add a WebJob in an App Service app in the portal (manually triggered WebJob).

  3. Fill in the Add WebJob settings as specified in the table, then select Create Webjob.

    Screenshot that shows how to configure a manually triggered WebJob for an App Service app.

    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 must not contain special characters other than "-" and "_".
    File Upload ConsoleApp1.zip A .zip file that contains your executable or script file and 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
  4. 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.

  5. To run a manually triggered WebJob, right-click the WebJob in the list and select the Run button, then confirm your selection.

    Screenshot that shows how to run a manually triggered WebJob in the Azure portal.

Create a scheduled WebJob

A scheduled Webjob is also triggered. You can schedule the trigger to occur automatically on the schedule you specify.

  1. In the Azure portal, go to the App Service page of your App Service web app, API app, or mobile app.

  2. From the left pane, select WebJobs, then select Add.

    Screenshot that shows how to add a WebJob in an App Service app in the portal (scheduled WebJob).

  3. Fill in the Add WebJob settings as specified in the table, then select Create Webjob.

    Screenshot that shows how to configure a scheduled WebJob in an App Service app.

    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 must not contain special characters other than "-" and "_".
    File Upload ConsoleApp.zip A .zip file that contains your executable or script file and 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.
  4. 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.

  5. The scheduled WebJob is run at the schedule defined by the CRON expression. To run it manually at anytime, right-click the WebJob in the list and select the Run button, then confirm your selection.

    Screenshot that shows how to run a manually scheduled WebJob in the Azure portal.

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. 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. You can use this method 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

  1. For the WebJob you want to see, select Logs.

    Screenshot that shows how to access logs for a WebJob.

  2. In the WebJob Details page, select a time to see details for one run.

    Screenshot that shows how to choose a WebJob run to see its detailed logs.

  3. In the WebJob Run Details page, you can select download to get a text file of the logs, or select the WebJobs breadcrumb link at the top of the page to see logs for a different WebJob.

WebJob statuses

The following is a list of common WebJob statuses:

  • Initializing The app has started and the WebJob is going through its initialization process.
  • Starting The WebJob is starting up.
  • Running The WebJob is running.
  • PendingRestart A continuous WebJob exits in less than two minutes since it started for any reason, and App Service waits 60 seconds before restarting the WebJob. If the continuous WebJob exits after the two-minute mark, App Service doesn't wait the 60 seconds and restarts the WebJob immediately.
  • Stopped The WebJob was stopped (usually from the Azure portal) and is currently not running and won't run until you start it again manually, even for a continuous or scheduled WebJob.
  • Aborted This can occur for many of reasons, such as when a long-running WebJob reaches the timeout marker.

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.