Run Background tasks with WebJobs

6 min to read Contributors

Overview

You can run programs or scripts in WebJobs in your Azure App Service web app in three ways: on demand, continuously, or on a schedule. There is no additional cost to use WebJobs.

This article shows how to deploy WebJobs by using the Azure Portal. For information about how to deploy by using Visual Studio or a continuous delivery process, see How to Deploy Azure WebJobs to Web Apps.

The Azure WebJobs SDK simplifies many WebJobs programming tasks. For more information, see What is the WebJobs SDK.

Azure Functions provides another way to run programs and scripts from either a serverless environment or from an App Service app. For more information, see Azure Functions overview.

Note

Although this article refers to web apps, it also applies to API apps and mobile apps.

Acceptable file types for scripts or programs

The following file types are accepted:

  • .cmd, .bat, .exe (using windows cmd)
  • .ps1 (using powershell)
  • .sh (using bash)
  • .php (using php)
  • .py (using python)
  • .js (using node)
  • .jar (using java)

Create an on demand WebJob in the portal

  1. In the Web App blade of the Azure Portal, click All settings > WebJobs to show the WebJobs blade.

    WebJob blade

  2. Click Add. The Add WebJob dialog appears.

    Add WebJob blade

  3. Under Name, provide a name for the WebJob. The name must start with a letter or a number and cannot contain any special characters other than "-" and "_".
  4. In the How to Run box, choose Run on Demand.
  5. In the File Upload box, click the folder icon and browse to the zip file that contains your script. The zip file should contain your executable (.exe .cmd .bat .sh .php .py .js) as well as any supporting files needed to run the program or script.
  6. Check Create to upload the script to your web app.

    The name you specified for the WebJob appears in the list on the WebJobs blade.

  7. To run the WebJob, right-click its name in the list and click Run.

    Run WebJob

Create a continuously running WebJob

  1. To create a continuously executing WebJob, follow the same steps for creating a WebJob that runs once, but in the How to Run box, choose Continuous.
  2. To start or stop a continuous WebJob, right-click the WebJob in the list and click Start or Stop.
Note

If your web app runs on more than one instance, a continuously running WebJob will run on all of your instances. On-demand and scheduled WebJobs run on a single instance selected for load balancing by Microsoft Azure.

For Continuous WebJobs to run reliably and on all instances, enable the Always On* configuration setting for the web app otherwise they can stop running when the SCM host site has been idle for too long.

Create a scheduled WebJob using a CRON expression

This technique is available to Web Apps running in Basic, Standard or Premium mode, and requires the Always On setting to be enabled on the app.

To turn an On Demand WebJob into a scheduled WebJob, simply include a settings.job file at the root of your WebJob zip file. This JSON file should include a schedule property with a CRON expression, per example below.

The CRON expression is composed of 6 fields: {second} {minute} {hour} {day} {month} {day of the week}.

For example, to trigger your WebJob every 15 minutes, your settings.job would have:

{
    "schedule": "0 */15 * * * *"
}

Other CRON schedule examples:

  • Every hour (i.e. whenever the count of minutes is 0): 0 0 * * * *
  • Every hour from 9 AM to 5 PM: 0 0 9-17 * * *
  • At 9:30 AM every day: 0 30 9 * * *
  • At 9:30 AM every week day: 0 30 9 * * 1-5

Note: when deploying a WebJob from Visual Studio, make sure to mark your settings.job file properties as 'Copy if newer'.

Create a scheduled WebJob using the Azure Scheduler

The following alternate technique makes use of the Azure Scheduler. In this case, your WebJob does not have any direct knowledge of the schedule. Instead, the Azure Scheduler gets configured to trigger your WebJob on a schedule.

The Azure Portal doesn't yet have the ability to create a scheduled WebJob, but until that feature is added you can do it by using the classic portal.

  1. In the classic portal go to the WebJob page and click Add.
  2. In the How to Run box, choose Run on a schedule.

    New Scheduled Job

  3. Choose the Scheduler Region for your job, and then click the arrow on the bottom right of the dialog to proceed to the next screen.
  4. In the Create Job dialog, choose the type of Recurrence you want: One-time job or Recurring job.

    Schedule Recurrence

  5. Also choose a Starting time: Now or At a specific time.

    Schedule Start Time

  6. If you want to start at a specific time, choose your starting time values under Starting On.

    Schedule Start at a Specific Time

  7. If you chose a recurring job, you have the Recur Every option to specify the frequency of occurrence and the Ending On option to specify an ending time.

    Schedule Recurrence

  8. If you choose Weeks, you can select the On a Particular Schedule box and specify the days of the week that you want the job to run.

    Schedule Days of the Week

  9. If you choose Months and select the On a Particular Schedule box, you can set the job to run on particular numbered Days in the month.

    Schedule Particular Dates in the Month

  10. If you choose Week Days, you can select which day or days of the week in the month you want the job to run on.

    Schedule Particular Week Days in a Month

  11. Finally, you can also use the Occurrences option to choose which week in the month (first, second, third etc.) you want the job to run on the week days you specified.

    Schedule Particular Week Days on Particular Weeks in a Month

  12. After you have created one or more jobs, their names will appear on the WebJobs tab with their status, schedule type, and other information. Historical information for the last 30 WebJobs is maintained.

    Jobs list

Scheduled jobs and Azure Scheduler

Scheduled jobs can be further configured in the Azure Scheduler pages of the classic portal.

  1. On the WebJobs page, click the job's schedule link to navigate to the Azure Scheduler portal page.

    Link to Azure Scheduler

  2. On the Scheduler page, click the job.

    Job on the Scheduler portal page

  3. The Job Action page opens, where you can further configure the job.

    Job Action PageInScheduler

View the job history

  1. To view the execution history of a job, including jobs created with the WebJobs SDK, click its corresponding link under the Logs column of the WebJobs blade. (You can use the clipboard icon to copy the URL of the log file page to the clipboard if you wish.)

    Logs link

  2. Clicking the link opens the details page for the WebJob. This page shows you the name of the command run, the last times it ran, and its success or failure. Under Recent job runs, click a time to see further details.

    WebJobDetails

  3. The WebJob Run Details page appears. Click Toggle Output to see the text of the log contents. The output log is in text format.

    Web job run details

  4. To see the output text in a separate browser window, click the download link. To download the text itself, right-click the link and use your browser options to save the file contents.

    Download log output

  5. The WebJobs link at the top of the page provides a convenient way to get to a list of WebJobs on the history dashboard.

    Link to WebJobs list

    List of WebJobs in history dashboard

    Clicking one of these links takes you to the WebJob Details page for the job you selected.

Notes

  • Web apps in Free mode can time out after 20 minutes if there are no requests to the scm (deployment) site and the web app's portal is not open in Azure. Requests to the actual site will not reset this.
  • Code for a continuous job needs to be written to run in an endless loop.
  • Continuous jobs run continuously only when the web app is up.
  • Basic and Standard modes offer the Always On feature which, when enabled, prevents web apps from becoming idle.
  • You can only debug continuously running WebJobs. Debugging scheduled or on-demand WebJobs is not supported.

Next Steps

For more information, see Azure WebJobs Recommended Resources.