Managing Cron Jobs
Learn how to manage Cron Jobs effectively in Vercel. Explore cron job duration, error handling, deployments, concurrency control, local execution, and more to optimize your serverless workflows.Cron Jobs are available on all plans
To view your active cron jobs:
- Select your project from the Vercel dashboard
- Select the Settings tab
- Select the Cron Jobs tab from the left sidebar
- Updating Cron Jobs: Change the expression in
vercel.json
file or the function's configuration, and then redeploy - Deleting Cron Jobs: Remove the configuration from the
vercel.json
file or the function's configuration, and then redeploy - Disabling Cron Jobs: Navigate to the Cron Jobs tab and then click the Disable Cron Jobs button
Disabled cron jobs will still be listed and will count towards your cron jobs limits
It is possible to secure your cron job invocations by adding an environment variable called CRON_SECRET
to your Vercel project. We recommend using a random string of at least 16 characters for the value of CRON_SECRET
. A password generator, like 1Password, can be used to create one.
The value of the variable will be automatically sent as an Authorization
header when Vercel invokes your cron job. Your endpoint can then compare both values, the authorization header and the environment variable, to verify the authenticity of the request.
import type { NextRequest } from 'next/server';
export function GET(request: NextRequest) {
const authHeader = request.headers.get('authorization');
if (authHeader !== `Bearer ${process.env.CRON_SECRET}`) {
return new Response('Unauthorized', {
status: 401,
});
}
return Response.json({ success: true });
}
The authorization
header will have the Bearer
prefix for the value.
For those using TypeScript versions below 5.2, it's important to adapt the code to import NextResponse from 'next/server'
and use NextResponse.json
for the response. This ensures compatibility with earlier TypeScript versions in Next.js applications. In TypeScript 5.2 and above, the standard new Response
pattern should be used.
The duration limits for Cron jobs are identical to those of Serverless and Edge functions. See the maxDuration
documentation for more information.
In most cases, these limits are sufficient. However, if you need more processing time, it's recommended to split your cron jobs into different units or distribute your workload by combining cron jobs with regular HTTP requests with your API.
Vercel will not retry an invocation if a cron job fails. You can check for error logs through the View Log button in the Cron Jobs tab.
Cron jobs can be created for dynamic routes:
{
"crons": [
{
"path": "/api/sync-slack-team/T0CAQ10TZ",
"schedule": "0 5 * * *"
},
{
"path": "/api/sync-slack-team/T4BOE34OP",
"schedule": "0 5 * * *"
}
]
}
If you create a cron job for a path that doesn't exist, it generates a 404 error. However, Vercel still executes your cron job. You can analyze your logs to check if there are any issues.
Creating a new deployment will not interrupt your running cron jobs; they will continue until they finish.
When performing an instant rollback, all cron jobs will be reverted to the state of the rolled back deployment.
When a cron job takes too long to run, you might encounter two concurrent cron job invocations, which could cause problems. Implementing a lock mechanism in your cron job, for example by using Redis, is recommended in such cases.
Alternatively, ensure your cron job is fast enough or set it to timeout if it runs for too long.
Cron jobs are API routes. You can run them locally by making a request to their endpoint. For example, if your cron job is in /api/cron
, you could visit the following endpoint in your browser: http://localhost:3000/api/cron
. Note, there's currently no support for vercel dev
, next dev
, or other framework-native local development servers.
Cron jobs do not follow redirects. When a cron-triggered endpoint returns a 3xx redirect status code, the job completes without further requests. Redirect responses are treated as final for each invocation.
The view logs button on the cron job overview can be used to verify the response of the invocations and gain further insights.
Cron jobs are logged as function invocations from the Logs tab of your projects dashboard. See how to view runtime logs for more information.
Was this helpful?