/

How to Set Up \"Cloud Cron Jobs\" using Netlify Scheduled Functions

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:

  1. Create a serverless function in netlify/functions directory in your repository. For example, you can create a file named test.js:
1
2
3
4
exports.handler = (event, context) => {
// do something
return { statusCode: 200 };
}
  1. 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
2
[functions."test"]
schedule = "@hourly"

Alternatively, you can set the schedule directly in the function itself without the need for an entry in netlify.toml:

1
2
3
4
5
6
const handler = (event, context) => {
// do something
return { statusCode: 200 };
}

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

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”]