The Timer trigger allows you to run Azure Functions on a schedule. It uses a cron-like expression to define the schedule.
A Timer trigger is a type of Azure Function binding that enables your function to execute at regular intervals. This is incredibly useful for tasks like:
The Timer trigger is configured in the function.json
file for your function.
timerTrigger
in
myTimer
).
@hourly
, @daily
, @weekly
, @monthly
.
function.json
{
"scriptFile": "../run.py",
"bindings": [
{
"schedule": "0 */5 * * * *",
"name": "myTimer",
"type": "timerTrigger",
"direction": "in"
}
]
}
In this example, "schedule": "0 */5 * * * *"
means the function will run every 5 minutes.
The cron expression follows a 6-field format:
{second} {minute} {hour} {day of month} {month} {day of week}
For example:
0 0 10 * * *
: Run at 10:00 AM every day.0 30 14 1 * *
: Run at 2:30 PM on the 1st of every month.0 0 12 * * 1
: Run at 12:00 PM every Monday.You can find detailed information on cron expressions and Azure Functions scheduling here.
The Timer trigger provides information about the elapsed time and the next scheduled execution time.
__init__.py
)import datetime
import logging
import azure.functions as func
def main(myTimer: func.TimerRequest) -> None:
utc_timestamp = datetime.datetime.utcnow().isoformat()
if myTimer.past_due:
logging.info('The timer is past due!')
logging.info('Python timer trigger function executed at %s', utc_timestamp)
logging.info('Next timer fire at: %s', myTimer.next_fire_time)
TimerTrigger.cs
)using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
public static class TimerTrigger
{
[FunctionName("TimerTriggerCSharp")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.UtcNow}");
if (myTimer.IsPastDue)
{
log.LogInformation("C# Timer trigger: Timer is past due!");
}
log.LogInformation($"Next timer to fire at: {myTimer.ScheduleStatus.Next}");
}
}