Why you shouldn’t use “Cron Jobs?”

Why you shouldn’t use “Cron Jobs?”

Author: The Carta Team
|
Read time:  2 minutes
Published date:  October 5, 2016
|
Updated date:  September 20, 2023
Cron isn't always the best way to solve a software development problem.

Executing a task on an interval or at a specific time is a common problem with application developers.

Many software developers think, I know how to solve this, I’ll use cron.

A few problems with cron:

  1. Smallest resolution is 1 minute—If a task needs to run every 30 seconds, you can’t do it with cron.

  2. Error handling—If a job fails, what should happen? Solutions have been built to solve this single problem. Developers love adding more band-aids rather than admitting there is a better way. Deja Vu?

  3. Logging—Crons don’t log, unless you tell them too. It is rare to see people log output of their cron jobs.

  4. Working with cron pulls you out of the application—cron is a system level process. Not an application process. It’s challenging to give application developers access to anything at a system level. Developers shouldn’t care where their application runs… A good example of this is timezones. If a system person changes the timezone of a server, the cron may run at a different time than expected. The less the app developers have to worry about what they run on, the better.

  5. Not enough reasons? You can read more about how cron doesn’t work at this StackExchange thread.

There is a better way.

Background jobs and a scheduler

Developers of Web Applications are aware of a common problem of offloading a task that can hold up a web request by putting it into a background job. Python’s most famous background task worker is Celery. Celery has a built in feature called Beat, which is a scheduler.

Here is how it works.

Create a celery task. This is the same type of task you create when doing background work.

Then add it to the Beat scheduler. Celery Beat is a service which runs and at regular intervals and puts things onto a celery queue.

You however, aren’t limited to “cron style” jobs. You can also run things on regular intervals.

ProTip™—Tasks can call other tasks.

Please, hear my cry:

Stop using cron for regular application tasks.