← flaviocopes.com

How to set up "cloud cron jobs" using Netlify Scheduled Functions

Published Apr 28 2022

Psssst! The 2023 WEB DEVELOPMENT BOOTCAMP is starting on FEBRUARY 01, 2023! SIGNUPS ARE NOW OPEN to this 10-weeks cohort course. Learn the fundamentals, HTML, CSS, JS, Tailwind, React, Next.js and much more! ✨

Netlify Scheduled Functions allow us to do some interesting things.

Here’s how to set them up.

Create a serverless function in netlify/functions in your repository, for example test.js:


exports.handler = (event, context) => {
  //do something
  return { statusCode: 200 }

Then in netlify.toml (create this file if you don’t have it yet) configure how frequently you want this Netlify Scheduled Function to run:

schedule = "@hourly"

Alternatively you can set this in the function itself, with no need for this entry:

const handler = (event, context) => {
  //do something
  return { statusCode: 200 }

exports.handler = schedule('@hourly', handler)

@hourly runs every hour at minute 0 @daily runs every day at 00:00 @weekly runs every Sunday at 00:00

@monthly and @yearly are available too.

You can also use a cron expression, like 5 4 * * * or any other expression (crontab guru is your friend)

You can also invoke a function manually using netlify functions:invoke test where test is the name of the function.

You can use Netlify Scheduled Functions for many different use cases.

I set a Netlify Scheduled Function to auto-deploy the repository every day to post a schedule blog post, for which I set the publishing date in advance.

I use the Fetch API to call the deploy webhook so I can do automatic deploys on Netlify.

I write books for developers 👇🏼

© 2023 Flavio Copes Flavio Copes made in Italy 🇮🇹 using Notion to Site Notion to Site