---
title: "【Jenkins】Jenkins cron syntax"
author: "Perrin Yong"
author_profile: https://www.pystone.net/profile/
published_by: "Perrin Yong"
canonical: https://www.pystone.net/notes/jenkins-cron-syntax/
type: note
content_role: unspecified
visibility: public
id_stability: rename-stable
source_path: "10-计算机、信息技术与工程/03-软件工程与质量保障/CI-CD与质量保障/CI-CD工具链/【Jenkins】Jenkins cron syntax.md"
content_hash: d7bb6b6d3d83178f497bcaa8c63332ef553ac886cc3ddd57df9c7fb2fb052446
knowledge_version: 224c990773de.5fa8af6e39fa
site_commit: 224c990773de166d23a886306577dd90379529ce
notes_commit: 5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a
---
# 【Jenkins】Jenkins cron syntax

﻿# 【Jenkins】Jenkins cron syntax

> 创建时间：2021/6/20 20:01

* Jenkins cron syntax
    * Introduction of cron
    * 用法
      * Hash
      * */3与H/3
      * 关于DOM与DOW的冲突
      * Nonstandard predefined scheduling definitions
    * 例子
  * Ref

## Jenkins cron syntax

### Introduction of cron

The software utility **cron** also known as cron job is a time-based job scheduler in Unix-like computer operating systems. Users who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.

### 用法

Each line consists of 5 fields separated by TAB or whitespace:

Each of these fields can be either an **asterisk** (meaning all valid values), **an element** , or **a list of elements separated by commas**.

```csharp
string a = &quot;test&quot;;
string tmp = &quot;est&quot;;
string b = &quot;t&quot; + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

```

  * `*` specifies all valid values
  * M-N specifies a range of values
  * `M-N/X` or `*/X` steps by intervals of X through the specified range or whole valid range
  * `A,B,…,Z` enumerates multiple values

#### Hash

目的： **To produce even load on the system**
使用随机的执行时间，保持系统的负载均衡。例如：避免每天执行的任务，都在每天0点0分执行。

For example, using 0 0 * * * for a dozen daily jobs will cause a large **spike** at midnight. In contrast, using H H * * * would still execute each job once a day, but not all at the same time, better using limited resources.

> The H symbol can be thought of as a random value over a range, but it actually is a **hash** of the job name, **not a random function** , so that the value remains stable for any given project.

#### */3与H/3

Beware that for the day of month field, short cycles such as */3 or H/3 will not work consistently near the end of most months, due to variable month lengths. For example, */3 will run on the 1st, 4th, …31st days of a long month, then again the next day of the next month. Hashes are always chosen in the 1-28 range, so H/3 will produce a gap between runs of between 3 and 6 days at the end of a month. (Longer cycles will also have inconsistent lengths but the effect may be relatively less noticeable.)

#### 关于DOM与DOW的冲突

If either the month or day of month is specified as an element or list, and the day of week is also specified as an element or list, then any day matching either the month and day of month, or the day of week, shall be matched.

#### Nonstandard predefined scheduling definitions

![assets/0024 - 【Jenkins】Jenkins cron syntax__resource-001-657186b03182.png](/media/393090d13c743af5f507.png)

> Jenkins中的情况不太一样：
>  `@yearly`, `@annually`, `@monthly`, `@weekly`, `@daily`, `@midnight`, and `@hourly`
>  These use the hash system for automatic balancing. For example, @hourly is the same as H * * * * and could mean at any time during the hour. @midnight actually means some time between 12:00 AM and 2:59 AM.

### 例子

  1. Runs at 23:45 (11:45 PM) every Saturday.

```csharp
string a = &quot;test&quot;;
string tmp = &quot;est&quot;;
string b = &quot;t&quot; + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

```
  2. every ten minutes in the first half of every hour

```csharp
string a = &quot;test&quot;;
string tmp = &quot;est&quot;;
string b = &quot;t&quot; + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

```
  3. once every two hours at 45 minutes past the hour starting at 9:45 AM and finishing at 3:45 PM every weekday.

```csharp
string a = &quot;test&quot;;
string tmp = &quot;est&quot;;
string b = &quot;t&quot; + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

```
  4. once in every two hours slot between 9 AM and 5 PM every weekday (perhaps at 10:38 AM, 12:38 PM, 2:38 PM, 4:38 PM)

```csharp
string a = &quot;test&quot;;
string tmp = &quot;est&quot;;
string b = &quot;t&quot; + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

```

![assets/0024 - 【Jenkins】Jenkins cron syntax__resource-002-57a0ece739ef.png](/media/a25de05a0c8ed7171fce.png)

图片来源：https://www.jenkins.io/doc/book/pipeline/syntax/

## Ref

https://en.wikipedia.org/wiki/Cron
https://www.jenkins.io/doc/book/pipeline/syntax/
https://pubs.opengroup.org/onlinepubs/007904975/utilities/crontab.html
