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.
You can use App Router Route Handlers to secure your cron jobs, even when using the Pages Router.
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
. You should be aware that while your browser may follow redirects, cron job invocations in production will not follow redirects.
There is 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. You can view the logs for a cron job from the list on the Cron jobs settings page of the project:
- From the list of cron jobs, select View Logs.
- This will take you to the runtime logs view with a
requestPath
filter to your cron job such asrequestPath:/api/my-cron-job
.
See how to view runtime logs for more information.
Note that when cron jobs respond with a redirect or a cached response, they will not be shown in the logs.
Hobby users can only create cron jobs with hourly accuracy. Vercel may invoke these cron jobs at any point within the specified hour to help distribute load across all accounts. For example, an expression like * 8 * * *
could trigger an invocation anytime between 08:00:00
and 08:59:59
.
For all other teams, cron jobs will be invoked within the minute specified. For instance, the expression 5 8 * * *
would trigger an invocation between 08:05:00
and 08:05:59
.
Was this helpful?