Check out our new GPU Glossary! Read now
April 30, 20243 minute read
How to run cron jobs
author

What is a cron job?

A cron job is a scheduled task. You can use cron jobs to automate work by running a script at a regular interval (e.g. every hour, every day).

There are many use cases for cron jobs:

  • Running data jobs
  • Sending reports or alerts
  • System maintenance e.g. removing old Docker images

Cron syntax

A cron schedule is specified with 5 numbers that specify (in order):

  • Minute (0-59)
  • Hour (0-23)
  • Day of month (1-31)
  • Month (1-12)
  • Day of week (0-6)

The most common configuration you’ll see is * * * * *, which means “run every minute”. The second most common configuration is something like 0 * * * *, which means “run hourly” or more specifically “run at minute zero of every hour”.

Cron uses the time zone of the host machine, which is likely UTC.

Here’s some other examples:

Cron Example Explanation Syntax Note
0,30 * * * * Twice an hour, once at the 0 minute mark and again at the 30 minute mark , means “and”
0 6 * * 0 Every Sunday at 6am UTC Last value is day of week (0=Sunday)
* * 1 3,9 * Every minute on the 1st of March and September 3rd and 4th values represent day of month and the month
* * * * 1-5 Every minute, Mon-Fri - defines a range of values
*/5 * * * * Every 5 minutes / defines step values or intervals

crontab.guru is a great tool to double check your cron expressions.

Crontab

The crontab is the file that specifies all your cron jobs using the cron syntax described above. On computers with a Unix-like operating system (e.g. MacOS, most cloud servers), you can add a cron job to the crontab by typing crontab -e from your terminal and adding your job in the format <cron schedule syntax> <path to executable script>. For example,

*/5 * * * * /Users/modal/run_job.sh

Once this is added to the crontab, your computer will execute the /Users/modal/run_job.sh script every 5 minutes.

Limitations of cron

Primarily made for shell scripts

This makes running data jobs annoying since they are most often written in Python or SQL and require additional configuration to get working.

No monitoring

Cron jobs do not provide notifications or output when a job fails.

Requires cloud provisioning

If you want to run cron jobs in the cloud, you need to set up an EC2 and go through the manual process of uploading your script, editing the crontab remotely, making sure the script stays updated, etc.

An alternative to cron jobs are orchestrator tools like Airflow, but this is often overkill for simple scheduled tasks.

How to run cron jobs on Modal

We believe Modal is the best way to run cron jobs in the cloud. Write your Python code locally, and decorate it with a modal.Period:

@app.function(schedule=modal.Period(days=1))
def f():
    print("This function will run every day")

Or if you prefer using cron syntax, you can attach that as well:

@app.function(schedule=modal.Cron("0 0 * * *"))
def f():
    print("This function will run every day at midnight UTC")

You can now schedule your Python script directly and get basic monitoring, all without having to touch any cloud providers or edit any crontab files.

Examples (built on Modal cron jobs)

Conclusion

Cron has been around for decades and is one of the most battle-tested pieces of software out there. You can’t go wrong using cron jobs for simple scheduled tasks, and Modal has made it even easier to manage cron jobs for your data and automation needs.

Ship your first app in minutes.

Get Started

$30 / month free compute