Enable & Write Cron Jobs for Strapi

Enable & Write Cron Jobs for Strapi

A cron job is a useful utility that can be found in any unix-like operating system. It can be used to schedule time-based commands without human intervention. With Strapi, we can use cron jobs to import data or run application tasks automatically.

To write cron jobs in Strapi is super simple! In this tutorial I will show you how you can enable and write cron jobs for Strapi.

1. Enable Cron Jobs for Strapi

To write cron jobs in Strapi, we first need to enable them. In earlier versions of Strapi this was done via admin dashboard, however this is no longer the case. In the later versions of Strapi we have to edit the config/server.js file in our project.

// config/server.js

cron: { enabled: true }

A more complete example might look like this.

// config/server.js

module.exports = ({ env }) => ({
  host: env('HOST', ''),
  port: env.int('PORT', 1337),
  admin: {
    auth: {
      secret: env('ADMIN_JWT_SECRET', 'SomeSecretKey'),
  cron: { enabled: true },

2. Write Cron Jobs for Strapi

Cron jobs in Strapi are written in the config/functions/cron.js. Below you have an example of how to create a cron job and console.log it to your terminal.

// config/functions/cron.js

'use strict';

 * Cron config that gives you an opportunity
 * to run scheduled jobs.
 * The cron format consists of:
 * See more details here: https://strapi.io/documentation/v3.x/concepts/configurations.html#cron-tasks

module.exports = {
  '*/1 * * * *': async() => {
    console.log("I am a cron job and I ran " + new Date());

3. Understand the Cron Job Format

To write timely cron jobs you need to have a basic understanding how the cron format works. Below you can find a diagram with the format laid out.

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

I hope this tutorial helped you get up and running with cron jobs for Strapi, and hopefully automate some of your repetitive tasks. I have also written a few other Strapi tutorials that you might find interesting.

Freddie Freddie 4 years, 5 months ago 0
Login to Comment
No comments have been posted yet, be the first one to comment.
How to Install Deno on macOS
How to Install Deno on macOS
Deno is a simple, modern and secure runtime for JavaScript and TypeScript, by the creator of Node.js himself, Ryan Dahl. Deno uses the Chrome v8 engine and is built with Rust. The project just reach version 1.0 and got many people in the JavaScript community interested. In this tutori...
How to Install & Configure Strapi with PostgreSQL
How to Install & Configure Strapi with PostgreSQL
If you have used Strapi before, you know how fast and easy it is to install with the "--quickstart" flag that includes SQLite as a default database. However, if you want to use a database like MongoDB or PostgreSQL the install and configuration requires a little bit more att...
How to Build a Movie Database & API with Strapi
How to Build a Movie Database & API with Strapi
Strapi is an awesome headless CMS built with Node.js that can speed up the process of building an API quiet dramatically. It's perfect for people who enjoy the frontend more than the backend, and it allows you to build complex database structures with out writing any code. Magic, if y...