Wake Me Up When September Ends.

A wanderer capable of grasping the beauty of the grass, with a heart full of ancient charm, and a fondness for playful wit. Those who understand my words are knowledgeable individuals; those who decipher my code truly comprehend the essence. I am a wandering code swordsman, carrying my skills and riding freely through the digital world.

🫡Cron 定时任务说明

时常记不住定时任务一些配置规则,所以这边记录一下!☘

Cron 定时任务是一种在 Unix/Linux 系统中用于执行预定时间间隔的任务的机制。它使用 cron 表达式来定义任务的执行时间。

Cron 表达式由空格分隔的五个字段组成,分别表示分钟、小时、日期、月份和星期几。每个字段可以接受不同的取值范围和特殊字符。

minute   hour   day   month   week   command     顺序:分 时 日 月 周

以下是每个字段的取值范围和特殊字符的说明:

分钟字段:取值范围是 0-59。

小时字段:取值范围是 0-23。

日期字段:取值范围是 1-31。

月份字段:取值范围是 1-12。

星期字段:取值范围是 0-7,其中 0 和 7 都表示星期日。

对于每个字段,可以使用以下特殊字符来定义更复杂的定时任务:

星号(*):表示匹配该字段的所有可能值。

逗号(,):用于分隔多个取值,表示匹配任意一个取值。

连字符(-):用于指定一个范围,表示匹配该范围内的所有值。

斜杠(/):用于指定一个步长,表示匹配该步长的所有值。

以下是一些常见的 cron 表达式示例:

  1. 0 0 * * *:每天的 00:00 执行一次任务。
  2. 0 12 * * 1-5:周一至周五的 12:00 执行一次任务。
  3. 0 0 1 * *:每个月的第一天的 00:00 执行一次任务。
  4. 0 */2 * * *:每隔两个小时执行一次任务。

Cron 定时任务是一种非常灵活和强大的调度机制,可以帮助自动化执行定期任务,例如备份、清理、数据同步等。