Cron every 10 minutes

April 07, 2021 No comments cron crontab Scheduler Quartz Spring

1. Introduction

The cron is a time-based function used for scheduling jobs. In this short article, we are going to present how to create a cron expression that will be used to run tasks every 10 minutes.

2. Cron expression every 10 minutes for crontab

A crontab is a special file in unix system with instructions for cron jobs. Each line in the crontab file contains six fields separated by a space followed by the command to be run. The cron expression for crontab daemons that execute task every 10 minutes looks like the following:


We can break down the expression into the following components:
  1. */10 - means every 10 minutes,
  2. * - every hour,
  3. * - every day of the month,
  4. * - every month,
  5. * - every day of the week.

Example crontabs:

Run PHP script every 10 minutes:
*/10 * * * * /usr/bin/php /home/username/public_html/cron.php >/dev/null 2>&1

Create MySQL dump every 10 minutes:
*/10 * * * * mysqldump -u root -pPASSWORD database > /root/db.sql >/dev/null 2>&1

Run bash script every 10 minutes:
*/10 * * * * /bin/bash /home/username/backup.sh >/dev/null 2>&1

3. Cron expression every 10 minutes for Spring Scheduler

In Spring scheduler a cron expression contains six sequential fields: second, minute, hour, day of the month, month, day(s) of the week. In Spring cron expression use to run tasks in 10-minute intervals looks like the following:


Let's break down the expression into separate components:
  1. 0 - at second :00,
  2. 0/10 - every 10 minutes starting at minute :00,
  3. * - every hour,
  4. * - every day,
  5. * - every month,
  6. ? - any day of the week.

Example Spring scheduler configuration that executes task every 10 minutes could have the following structure:

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Component
public class MyScheduler {

    @Scheduled(cron = "0 0/10 * * * *")
    public void doSomething() {
        // this code will be executed every 10 minutes
    }

}

4. Cron expression every 10 minutes for Quartz

The Quartz project is an open-source task scheduling library that can be integrated with any Java application. Quartz in comparison to Spring scheduler has the additional 7th parameter in cron expression that stands for the year.


The following snippet creates a simple cron scheduler using Quartz library:

JobDetail job = newJob(SimpleJob.class)
    .withIdentity("job1", "group1")
    .build();

CronTrigger trigger = newTrigger()
    .withIdentity("trigger1", "group1")
    .withSchedule(cronSchedule("0 0/10 0 ? * * *"))
    .build();

sched.scheduleJob(job, trigger);

5. Conclusion

In this article we presented quick tip for creating cron expression that executes specific task every 10 minute. We created ready to copy/paste snippets for Spring applications, Quartz library, and linux crontab.
{{ message }}

{{ 'Comments are closed.' | trans }}