Cron explainer › Examples

Cron expression for every 2 hours

Every 2 hours, on the hour

SyntaxExpression
Unix crontab 0 */2 * * *
Spring @Scheduled 0 0 */2 * * *
Quartz 0 0 */2 * * ?

Open and edit in the explainer →

Next run times (UTC)

  1. Calculating…

Configuration examples

crontab

# crontab -e
0 */2 * * * /path/to/script.sh >> /var/log/job.log 2>&1

Runs in the server's system time zone. Check it with timedatectl.

Spring

@Scheduled(cron = "0 0 */2 * * *", zone = "UTC")
public void runJob() {
    // ...
}

Requires @EnableScheduling. Without zone, the server time zone is used.

Quartz

Trigger trigger = TriggerBuilder.newTrigger()
    .withIdentity("myTrigger")
    .withSchedule(CronScheduleBuilder.cronSchedule("0 0 */2 * * ?")
        .inTimeZone(TimeZone.getTimeZone("UTC")))
    .build();

Quartz always needs "?" in either day-of-month or day-of-week.

Kubernetes

apiVersion: batch/v1
kind: CronJob
metadata:
  name: my-job
spec:
  schedule: "0 */2 * * *"
  timeZone: "UTC"
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
            - name: job
              image: busybox
              command: ["sh", "-c", "echo hello"]

The timeZone field is GA since Kubernetes 1.27. Without it, the kube-controller-manager time zone (usually UTC) is used.

GitHub Actions

on:
  schedule:
    - cron: '0 */2 * * *'  # UTC

GitHub Actions always runs in UTC. Convert from UTC before using this.

More cron examples

Every minuteEvery 5 minutesEvery 10 minutesEvery 15 minutesEvery 30 minutesEvery hour (on the hour)Every 6 hoursEvery day at midnightEvery day at 9 AMEvery day at 6 PMTwice a day (9:00 and 18:00)Weekdays at 9 AMEvery 10 minutes during business hoursWeekends at 10 AMEvery Monday at 9 AMEvery Sunday at midnightFirst day of every month15th of every monthLast day of every monthLast weekday of every monthSecond Monday of every monthLast Friday of every monthEvery quarter (Jan, Apr, Jul, Oct 1st)Every year on January 1stEvery 10 secondsEvery 30 seconds