Skip to main content

Cron jobs

Cron jobs allow you to automate tasks on our platform, running scheduled processes like database cleanups, email notifications, or syncing data without manual intervention.

Cron jobs are available from the Cron Jobs section within the application view of the Control Panel. Note that cron jobs are not available on all Divio subscriptions and we allow further customisation on Business and Enterprise plans.

Cron job management

Creating tasks

Hit the Add Cron Job button within the "Cron Jobs" view and configure your command accordingly, here is an example:

  • Command: /usr/bin/bash -c "/app/runCommand.sh"
  • Frequency: Every day
  • Time, UTC: 22:45
  • Timeout: 600 seconds (only available on Business or Enterprise plans)

This will run the command every day at 22:45 UTC for about 10 minutes. The cron job will timeout if it runs longer than 10 minutes.

Deployment Required

After adding or changing a cron job, the respective environment must be redeployed to apply the configuration.

Task options

1

Command: The command runs in a new "cron" container but shares the same context/resources as your "web" container. This means it can access the same environment variables and code as the web container. However, the cron container isn't a full-fledged environment - it's not a bash shell, and commands relying on bash syntax won't work unless explicitly defined and exists in your Docker image. It can only execute specific commands, so you need to be precise to avoid ambiguity and unexpected behavior. In the example above, we defined the full path to bash and the script to run to ensure clarity.

2

Frequency/Time: You can schedule a cron job to run daily at a specific time, every hour at 15-minute intervals, or every 10 minutes. For the 10-minute interval option, the job will execute at :00, :10, :20, :30, :40, and :50 each hour.

3

Timeout: The default timeout is 10 minutes. You can adjust this value based on your requirements when subscribing to a (Business or Enterprise plan). If you're looking to run longer processes that need to be queued, we recommend using a more suitable task manager like Celery.

Each cron entry allows you to edit the job, view the Last run info, copy the command, or delete it.

Debugging

Cron jobs are shown within the Logs section and can be filtered by container type. We recommend adding print statements to your cron jobs to indicate when they start, show progress, and when they finish. This aids in debugging since there's no way to view real-time progress or SSH into the container running the cron job.

The Last run info in the cron job settings shows details such as the command executed, the start time, and the finish time.

Cron job logs

Cron jobs will also appear in the Metrics view, where you can monitor resource consumption. This includes details on how much memory the cron jobs are using, as well as their CPU usage. Monitoring these metrics is useful for identifying performance bottlenecks or resource limitations in your jobs, helping you optimize their efficiency.

Slower intervals for Cron Jobs

Our platform lets you define a frequency for every 10 minutes, every hour or every day. If you want to run a task at longer intervals, you can set the cron job execution time to daily and restrict the execution using a script.

For example, if you would like to <execute a command> every Thursday (4th day of the week), the script could look like this

sh -c 'if [ $(date +%u) -eq 4 ]; then <execute a command>; fi'

Or if you would like to <execute a command> every 5th day of the month, the script could look as follows.

sh -c 'if [ $(date +%d) -eq 5 ]; then <execute a command>; fi'