Skip to main content
Illustration by Annie Ruygt of Framkie the balloon wearing a sash and a prize ribbon in a gymnasium hall while a bird looks with admiration

Overview

crontab is a little too opinionated for containers—it’s a great little tool, but when run inside of containers it doesn’t grab ENV vars how we’d like it to. Fortunately there’s a Go binary called supercronic that’s a drop-in replacement for containers. The good news is that it’s pretty easy to get it running on Fly with a little bit of copy and pasting. Let’s get to it.

Create a crontab file

In the root of your project, add a crontab file.
If you need to run a job every 5 minutes, your crontab file would something like this:
Check out cron.help if you need a quick crontab syntax reference.

Install supercronic in the container

The latest releases for supercronic are on GitHub, where they include copy pasta 🍝instructions for getting it in your Dockerfile. As of January 2024, the current version of supercronic is v0.2.29. You’ll want to check the releases page for the latest version, but here’s what it looks like now:
After you put that in your Dockerfile, we’re going to configure Fly to run only one instance of supercronic. Why? Because if you have a cronjob that does something like deliver emails to customers, you only want them to get that once. The last thing you want is them getting as many emails from your services that matches the number of instances you’re running.

Setup a web & cron process

At the bottom of the fly.toml file, add the following:
Then we have to tell Fly that your web process matches up with a service by having this under the [[services]].
This change tells Fly that your web processes that you defined under [processes] will be exposed to the public internet. Your cron process won’t be exposed to the public Internet because it doesn’t need to!

Deploy & scale the new processes

Now that we’ve added supercronic to our Dockerfile, put the crontab at the root of our project folder, and reconfigured the fly.toml file, we’re ready to deploy to production.
Then we’ll need to scale the processes so that we only run one virtual machine container with the cron process. Be sure to change web to whatever number you had before.
That’s it! If all went well you now have cron running in a cron process in a Fly virtual machine. When you fly deploy it will get the latest code changes and reboot the virtual machines.

Resources