How to Set Up “Cloud Cron Jobs” using Netlify Scheduled Functions
Discover how to set up Netlify Scheduled Functions for your development needs.
Netlify Scheduled Functions provide the ability to perform various tasks. Follow these steps to set them up:
- Create a serverless function in
netlify/functions
directory in your repository. For example, you can create a file namedtest.js
:
1 | exports.handler = (event, context) => { |
- Open or create the
netlify.toml
file in your repository and configure the frequency at which you want the Netlify Scheduled Function to run. For example, if you want it to run every hour, add this configuration:
1 | [functions."test"] |
Alternatively, you can set the schedule directly in the function itself without the need for an entry in netlify.toml
:
1 | const handler = (event, context) => { |
Here are some common schedules you can use:
@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 also available.
You can also use a cron expression, such as 5 4 * * *
or any other expression. Crontab Guru is a helpful tool for creating cron expressions.
To manually invoke a function, you can use the command netlify functions:invoke test
, replacing test
with the name of your function.
Netlify Scheduled Functions can be used for a variety of purposes. For example, you can set up a function to automatically deploy your repository every day to post a scheduled blog post, with the publishing date set in advance.
To call the deploy webhook and achieve automatic deploys on Netlify, you can use the Fetch API.
tags: [“Netlify”, “Scheduled Functions”, “Serverless Functions”, “Cron Jobs”, “Cloud Computing”]