
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 acrontab file.
crontab file would something like this:
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. The example below uses v0.2.29. Check the releases page for the latest version and its checksum, and update SUPERCRONIC_URL and SUPERCRONIC_SHA1SUM to match:
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 thefly.toml file, add the following:
web process matches up with a service by having this under the [[services]].
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 addedsupercronic 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.
cron process. Be sure to change web to whatever number you had before.
cron process in a Fly virtual machine. When you fly deploy it will get the latest code changes and reboot the virtual machines.
Resources
RelatedāÆReading
- Task scheduling guide with CronāÆManager and friends A broader look at scheduling jobs on Fly.io ā covers CronāÆManager, Supercronic, scheduled Machines, and more.
- RunāÆmultipleāÆprocess groups in an app Explains how to define processes in
fly.toml, run multiple programs (web, cron, workers), scale by group. - App Configuration (
fly.toml) Inādepth look at the config file that orchestrates your services, processes, health checks, etc.