user3357926
user3357926

Reputation: 1137

How to scheduled a cron job in Next.js (not deploy and run in Vercel)?

How can I scheduled a cron job using cron together with Next.js 14 application? For example, I want to run a job every specific interval to update the database tables and etc.

It looks like many developers are configuring the cron jobs in vercel.json and run them in Vercel. Can we do the similar thing if not deploying our Next.js in Vercel like running in AWS EC2 or other server?

Upvotes: 1

Views: 1486

Answers (1)

Sukka
Sukka

Reputation: 332

Vercel's Cron Job feature makes an HTTP GET request to the deployed project's production deployment URL at the specified endpoint. The schedule field uses the same syntax as the crontab.

If you are deploying the code on AWS EC2, you can add the cronjob directly. And instead of specifying the endpoint directly, you can use curl in crontab.

The guide on how to set up a corn job using crontab: https://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job

Example

If you setup a cron job w/ Vercel Cron Job feature:

{
  "crons": [
    {
      "path": "/api/cron",
      "schedule": "0 5 * * *"
    }
  ]
}

The identical crontab would be:

0 5 * * * curl -s -o /dev/null https://your-project-id.vercel.app/api/cron

Upvotes: 1

Related Questions