How to set up "cloud cron jobs" using Netlify Scheduled Functions
Find out how to set up Netlify Scheduled Functions
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
:
netlify/functions/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:
[functions."test"]
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 wrote 17 books to help you become a better developer:
- C Handbook
- Command Line Handbook
- CSS Handbook
- Express Handbook
- Git Cheat Sheet
- Go Handbook
- HTML Handbook
- JS Handbook
- Laravel Handbook
- Next.js Handbook
- Node.js Handbook
- PHP Handbook
- Python Handbook
- React Handbook
- SQL Handbook
- Svelte Handbook
- Swift Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025