- Bill's Newsletter
- Posts
- Cron Jobs
Cron Jobs
How to create little jobs to automate tasks
For some of my apps I need to periodically run a process outside of user interactions. Some examples
Fetch weather from an API and store it in my database
Scrape websites to find links
Parse domain auction data
Remove old database data
In my cases, this can take anywhere from a few seconds to an hour.
These jobs enhance the apps I’m writing with ShipFast and open up new possibilities for what a single developer can build.
Enter Cron
Cron is a unix tool for running arbitrary jobs every x minutes, x days, x hours, etc. It runs automatically and unattended.
This is different from things you might do when a user initiates an action and is waiting for a response.
To setup a cron job you need two things: a command to run and a schedule of when to run it.
The command can be anything your operating system allows. On unix this might be a bash script or system command. On Vercel this would be a call to an API endpoint.
The schedule looks like this. You can even have multiple schedules running the same command if you need something more complicated.
Where To Host
Since I don’t want the pain of running my own VPS in the cloud, I almost always choose to run jobs on a serverless service.
I’ve used two different services and they have been able to handle everything I’ve needed so far.
Vercel Cron Jobs
These are the easiest to setup and on the Pro plan they have pretty generous limits. Vercel has good documentation.
First, create an API endpoint just like any other GET request. I use the App Router, so I’ll create a folder under /app/api called cron and put in a route.js.
In order to prevent unauthorized usage, you’ll want to create a secret key in your environment variables and check it
if (
req.headers.get('Authorization') !== `Bearer ${process.env.CRON_SECRET}`
) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
Second, create a vercel.json at your project’s top level and enter your command and schedule details
{
"crons": [
{
"path": "/api/cron",
"schedule": "*/15 * * * *"
}
]
}
All that’s left to do is check your code in, wait till the build is done, and turn on Cron Jobs in your Project Settings.
Vercel does have a limit on the length of time it will allow a job to run. By default this is 15 seconds. Each plan has a maximum duration as well.
Render Cron Jobs
Since I needed to run jobs that would take longer than the maximum allowed by Vercel I needed a different solution.
After getting tired of running a VPS I looked around and found Render.com. Render jobs can run up to 12 hours (which is way more than I need).
The pricing is pretty cheap too. I have a job that runs every morning for about 30 minutes. I put it on the Standard Plan to speed it up a bit.
This is my charge for 2 weeks of running and I’m paying about $4/month for all the jobs I’m running.
Setup is easy too. I created a NodeJS application - just a simple app.js of 50-100 lines of code to do what I need.
I check the project into GitHub like normal and Render will detect the changes and rebuild my project. My app basically has no build step, it just installs the node packages and the command is a regular npm run “x” command.
I’ve had no issues with Render in the few months I’ve been running jobs there. The process and pricing have been so good that I doubt I’ll look anywhere else.
Possibilities
Being able to quickly and easily create jobs that can run automatically and without the need for a user action opens up possibilities for new projects.
If you want to build faster as a solo developer check out ShipFast.