Azure Functions timer trigger

This article explains how to work with timer triggers in Azure Functions. A timer trigger lets you run a function on a schedule.

This is reference information for Azure Functions developers. If you're new to Azure Functions, start with the following resources:

Example

See the language-specific example:

C# example

The following example shows a precompiled C# function that runs every five minutes:

[FunctionName("TimerTriggerCSharp")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log)
{
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
}

C# script example

The following example shows a timer trigger binding in a function.json file and a C# script function that uses the binding. The function writes a log indicating whether this function invocation is due to a missed schedule occurrence.

Here's the binding data in the function.json file:

{
    "schedule": "0 */5 * * * *",
    "name": "myTimer",
    "type": "timerTrigger",
    "direction": "in"
}

Here's the C# script code:

public static void Run(TimerInfo myTimer, TraceWriter log)
{
    if(myTimer.IsPastDue)
    {
        log.Info("Timer is running late!");
    }
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}" );  
}

F# example

The following example shows a timer trigger binding in a function.json file and a F# script function that uses the binding. The function writes a log indicating whether this function invocation is due to a missed schedule occurrence.

Here's the binding data in the function.json file:

{
    "schedule": "0 */5 * * * *",
    "name": "myTimer",
    "type": "timerTrigger",
    "direction": "in"
}

Here's the F# script code:

let Run(myTimer: TimerInfo, log: TraceWriter ) =
    if (myTimer.IsPastDue) then
        log.Info("F# function is running late.")
    let now = DateTime.Now.ToLongTimeString()
    log.Info(sprintf "F# function executed at %s!" now)

JavaScript example

The following example shows a timer trigger binding in a function.json file and a JavaScript function that uses the binding. The function writes a log indicating whether this function invocation is due to a missed schedule occurrence.

Here's the binding data in the function.json file:

{
    "schedule": "0 */5 * * * *",
    "name": "myTimer",
    "type": "timerTrigger",
    "direction": "in"
}

Here's the F# script code:

module.exports = function (context, myTimer) {
    var timeStamp = new Date().toISOString();

    if(myTimer.isPastDue)
    {
        context.log('Node.js is running late!');
    }
    context.log('Node.js timer trigger function ran!', timeStamp);   

    context.done();
};

Attributes for precompiled C#

For precompiled C# functions, use the TimerTriggerAttribute, defined in NuGet package Microsoft.Azure.WebJobs.Extensions.

The attribute's constructor takes a CRON expression, as shown in the following example:

[FunctionName("TimerTriggerCSharp")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log)

You can specify a TimeSpan instead of a CRON expression if your function app runs on an App Service plan (not a Consumption plan).

Configuration

The following table explains the binding configuration properties that you set in the function.json file and the TimerTrigger attribute.

function.json property Attribute property Description
type n/a Must be set to "timerTrigger". This property is set automatically when you create the trigger in the Azure portal.
direction n/a Must be set to "in". This property is set automatically when you create the trigger in the Azure portal.
name n/a The name of the variable that represents the timer object in function code.
schedule ScheduleExpression On the Consumption plan, you can define schedules with a CRON expression. If you're using an App Service Plan, you can also use a TimeSpan string. The following sections explain CRON expressions. You can put the schedule expression in an app setting and set this property to a value wrapped in % signs, as in this example: "%NameOfAppSettingWithCRONExpression%". When you're developing locally, app settings go into the values of the local.settings.json file.

CRON format

A CRON expression for the Azure Functions timer trigger includes these six fields:

{second} {minute} {hour} {day} {month} {day-of-week}

Note

Many of the CRON expressions you find online omit the {second} field. If you copy from one of them, add the missing {second} field.

CRON time zones

The default time zone used with the CRON expressions is Coordinated Universal Time (UTC). To have your CRON expression based on another time zone, create a new app setting for your function app named WEBSITE_TIME_ZONE. Set the value to the name of the desired time zone as shown in the Microsoft Time Zone Index.

For example, Eastern Standard Time is UTC-05:00. To have your timer trigger fire at 10:00 AM EST every day, use the following CRON expression that accounts for UTC time zone:

"schedule": "0 0 15 * * *",

Alternatively, you could add a new app setting for your function app named WEBSITE_TIME_ZONE and set the value to Eastern Standard Time. Then the following CRON expression could be used for 10:00 AM EST:

"schedule": "0 0 10 * * *",

CRON examples

Here are some examples of CRON expressions you can use for the timer trigger in Azure Functions.

To trigger once every five minutes:

"schedule": "0 */5 * * * *"

To trigger once at the top of every hour:

"schedule": "0 0 * * * *",

To trigger once every two hours:

"schedule": "0 0 */2 * * *",

To trigger once every hour from 9 AM to 5 PM:

"schedule": "0 0 9-17 * * *",

To trigger At 9:30 AM every day:

"schedule": "0 30 9 * * *",

To trigger At 9:30 AM every weekday:

"schedule": "0 30 9 * * 1-5",

Usage

When a timer trigger function is invoked, the timer object is passed into the function. The following JSON is an example representation of the timer object.

{
    "Schedule":{
    },
    "ScheduleStatus": {
        "Last":"2016-10-04T10:15:00.012699+00:00",
        "Next":"2016-10-04T10:20:00+00:00"
    },
    "IsPastDue":false
}

Scale-out

The timer trigger supports multi-instance scale-out. A single instance of a particular timer function is run across all instances.

Next steps