跳到主要内容

Use cron jobs in your code

The strapi.cron object allows you to interact with CRON jobs (see configuration reference for more details).

Add or remove cron jobs

The cron object allows you to add cron jobs to the Strapi instance.

./src/plugins/my-plugin/strapi-server.js
module.exports = () => ({
bootstrap({ strapi }) {
strapi.cron.add({
// runs every second
myJob: {
task: ({ strapi }) => {
console.log("hello from plugin");
},
options: {
rule: "* * * * * *",
},
},
});
},
});

To remove a CRON job you can call the remove function on the strapi.cron object and pass in the key corresponding to the CRON job you want to remove.

✏️ Note

Cron jobs that are using the key as the rule can not be removed.

strapi.cron.remove("myJob");

List cron jobs

To list all the cron jobs that are currently running you can call the jobs array on the strapi.cron object.

strapi.cron.jobs