Cron Expression to Execute Every 10 Minutes

Cron Expression to Execute Every 10 Minutes

Cron expression is a time-based scheduling feature used in Unix-based systems and many other software applications. It allows you to automate tasks, schedule jobs, and run programs at specific intervals. One of the most popular requirements is to schedule the task to run every 10 minutes. In this article, we will show you how to create a Cron expression to execute a task every 10 minutes along with some sample code.

Understanding Cron Expression

Cron expression has six fields, separated by space or tab. These fields represent the time at which the task should be executed. The following is the format of the Cron expression:

* * * * * *
| | | | | |
| | | | | day of the week (0 - 7) (Sunday is 0 or 7)
| | | | month (1 - 12)
| | | day of the month (1 - 31)
| | hour (0 - 23)
| minute (0 - 59)

The asterisk (*) symbol indicates that the field should match all possible values. For example, 0 0 * * * * represents a task that should run at midnight (00:00) every day.

To execute a task every 10 minutes, we need to set the minute field to every 10 minutes. We should set the other fields to match any possible values. The following Cron expression can be used to run a task every 10 minutes:

*/10 * * * * *

The symbol */10 specifies that the task should be executed every 10 minutes, starting from minute 0. The other fields are set to match any possible value. This Cron expression will run the task every 10 minutes, regardless of the hour, day of the month, month, or day of the week.

Sample Code

Now let’s see how we can use this Cron expression in our code. The following sample code shows how to use the Cron expression with CronTrigger in Java:

import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class CronJobExample {
    public static void main(String[] args) {
        try {
            JobDetail job = JobBuilder.newJob(CronJob.class).withIdentity("CronJob").build();
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("CronTrigger")
                    .withSchedule(CronScheduleBuilder.cronSchedule("*/10 * * * * *")).build();
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();
            Scheduler scheduler = schedulerFactory.getScheduler();
            scheduler.start();
            scheduler.scheduleJob(job, trigger);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

In this code example, we create a job using JobBuilder and set the CronTrigger to execute the job every 10 minutes using the Cron expression we discussed above. The SchedulerFactory is used to create the Scheduler instance, and the job and trigger are scheduled using the Scheduler instance.

Conclusion

Cron expressions are an excellent way to schedule tasks and automate processes. By using the Cron expression */10 * * * * *, we can execute a task every 10 minutes with ease. The sample code we have looked at demonstrates how to use this Cron expression with CronTrigger in Java. Next time you need to schedule a task to execute every 10 minutes, use the Cron expression, and you will have it up and running in no time.

Like(0)
Linux Login Logout Command
Linux login commandLinux logout commandLinux nologin commandLinux exit commandLinux sulogin commandLinux rlogin commandLinux poweroff commandLinux ctrlaltdel CommandLinux shutdown commandLinux halt commandLinux reboot commandLinux init commandLinux runlevel commandLinux telinit command
Linux File Management Command
Linux cat commandLinux tac commandLinux nl commandLinux more commandLinux less commandLinux head commandLinux tail commandLinux rev commandLinux fold commandLinux fmt commandLinux expand commandLinux pr commandLinux sort commandLinux uniq commandLinux cut commandLinux comm commandLinux diff commandLinux join commandLinux diff3 commandLinux cmp commandLinux colrm commandLinux paste commandLinux mkdir commandLinux tr commandLinux split commandLinux csplit commandLinux tee commandLinux unexpand commandLinux patch commandLinux awk commandLinux sed commandLinux od commandLinux pwd commandLinux cd commandLinux ls commandLinux dir commandLinux dirs commandLinux touch commandLinux rmdir commandLinux cp commandLinux mv commandLinux rm commandLinux install commandLinux tmpwatch commandLinux file commandLinux du commandLinux wc commandLinux tree commandLinux cksum commandLinux md5sum commandLinux sum commandLinux dirname commandLinux mkfifo Command
Cron Expressions
Cron Expression to Run Every Day at 12 PMUnderstanding Vue Cron ExpressionsUnderstanding JS Cron ExpressionsA Comprehensive Guide to Cron Expressions for Scheduled TasksUnderstanding Linux Cron ExpressionsUnderstanding Quartz Cron ExpressionsCron ExpressionCron Time ExpressionCron Expression ParsingCron Expression: Executing a Task Every SecondCron Expression for Every Minute ExecutionCron Expression to Execute Every 10 MinutesCron Expression: Executing Every HourCron Expression to Execute Once a YearCron Expression: How to Schedule a Task to Run Daily at Midnight?