Running functions on a schedule
A timer trigger runs a function on a schedule written as an NCRONTAB expression. It replaces Windows scheduled tasks and cron jobs for things like nightly clean-ups, reports and data syncs.
- StartSchedule dueThe host checks the NCRONTAB schedule.
- Step 2Take the leaseOnly one instance gets the lease in Storage.
- Step 3Run the functionTimerInfo says whether the run is late.
- EndRecord next runThe schedule state is saved for the next run.
Schedules are in UTC by default.
The schedule expression
NCRONTAB has six fields: second, minute, hour, day of month, month and day of week.
- 0 */5 * * * *: every 5 minutes.
- 0 0 * * * *: at the start of every hour.
- 0 30 2 * * *: every day at 02:30.
- 0 0 9 * * 1-5: 09:00 Monday to Friday.
- Put the expression in a setting with %NightlySchedule% so each environment can differ.
A scheduled function
public class NightlyCleanup(ICleanupService cleanup, ILogger<NightlyCleanup> logger)
{
[Function("NightlyCleanup")]
public async Task Run([TimerTrigger("%NightlySchedule%")] TimerInfo timer, CancellationToken ct)
{
if (timer.IsPastDue)
logger.LogWarning("Running late; the app was not running at the scheduled time");
int removed = await cleanup.DeleteExpiredCartsAsync(ct);
logger.LogInformation("Removed {Count} expired carts", removed);
}
}Time zones and missed runs
Schedules are in UTC by default. 02:30 UTC is 08:00 in India, so write the time in UTC or set the WEBSITE_TIME_ZONE app setting where your plan supports it.
The runtime keeps the schedule in the Storage account. If the app was down when a run was due, the next run reports IsPastDue = true; your code decides whether to catch up.
Only one run at a time
The timer trigger uses a lease in Storage, so even when the app runs on several instances, a scheduled run happens on one instance only.
Make the job safe to run twice anyway (for example, delete only rows older than a date), because a failure half-way can lead to a retry by hand or a manual run.
RunOnStartup = true runs the function every time the host starts. It is handy for testing but avoid it in production, where scale-out and restarts would run the job at random times.
Interview question
How do you run an Azure Function every night at 2:30, and what should you watch out for?
I use a timer trigger with the NCRONTAB expression 0 30 2 * * *, ideally read from a setting. I remember that it is UTC unless a time zone is configured, check IsPastDue for missed runs, avoid RunOnStartup in production, and make the job idempotent so a re-run is safe.
Want the full details? Read the official Microsoft documentation for this topic.