From af267c3bfd14b5597cc6820f21e87c1ba0f568c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=81=E6=B6=9B?= <1841582040@qq.com> Date: Tue, 18 Jun 2024 21:39:20 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\345\212\241\347\256\241\347\220\206.md" | 335 ++++++++++++++++++ 1 file changed, 335 insertions(+) create mode 100644 "\351\273\204\346\265\201\346\266\233/20240617 \345\221\250\346\234\237\344\273\273\345\212\241\347\256\241\347\220\206.md" diff --git "a/\351\273\204\346\265\201\346\266\233/20240617 \345\221\250\346\234\237\344\273\273\345\212\241\347\256\241\347\220\206.md" "b/\351\273\204\346\265\201\346\266\233/20240617 \345\221\250\346\234\237\344\273\273\345\212\241\347\256\241\347\220\206.md" new file mode 100644 index 0000000..e2da77c --- /dev/null +++ "b/\351\273\204\346\265\201\346\266\233/20240617 \345\221\250\346\234\237\344\273\273\345\212\241\347\256\241\347\220\206.md" @@ -0,0 +1,335 @@ +# 笔记 + +## Cron的配置文件 + +在Debian系统中,Cron任务的配置文件通常分为以下几类: + +1. **用户级Cron任务**:每个用户都有自己的Cron任务配置文件,位于`/var/spool/cron/crontabs/username`。 +2. **系统级Cron任务**:系统级的Cron任务配置文件位于`/etc/crontab`和`/etc/cron.d/`目录下。 +3. **周期性任务目录**:这些目录包含在特定时间间隔运行的脚本,例如: + - `/etc/cron.hourly/` + - `/etc/cron.daily/` + - `/etc/cron.weekly/` + - `/etc/cron.monthly/` + +### 使用`crontab`命令 + +用户可以使用`crontab`命令来编辑、查看或删除自己的Cron任务【用户级】。 + +cron table 时间周期表,计划任务表 + +- **编辑Cron任务**: + + ```sh + crontab -e # edit + ``` + + 这将打开用户的Cron配置文件进行编辑。 + +- **查看Cron任务**: + + ```sh + crontab -l # list + ``` + + 这将列出用户当前的Cron任务。 + +- **删除Cron任务**: + + ```sh + crontab -r # remove + ``` + + 这将删除用户的所有Cron任务。 + +**Cron任务语法** + +Cron任务的格式如下: + +```sh +* * * * * command_to_execute # command_to_execute 可以是命令也可以是包含命令的文件 +- - - - - +| | | | | +| | | | +----- 星期几 (0 - 7) (星期天 = 0 或 7) +| | | +------- 月份 (1 - 12) +| | +--------- 日期 (1 - 31) +| +----------- 小时 (0 - 23) ++------------- 分钟 (0 - 59) +``` + +例如,要每天凌晨3点执行一个备份脚本,可以添加以下条目: + +```sh +0 3 * * * /path/to/backup_script.sh +``` + + + +### 使用`/etc/crontab` + +系统级的Cron任务可以直接编辑`/etc/crontab`文件。该文件具有一个额外的字段用于指定运行任务的用户: + +``` +# m h dom mon dow user command +17 * * * * root cd / && run-parts --report /etc/cron.hourly +25 6 * * 1 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) +47 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly ) +``` + +### 3.3 使用`/etc/cron.d/` + +在`/etc/cron.d/`目录下,可以创建单独的文件来定义Cron任务。每个文件的格式与`/etc/crontab`类似。 + +例如,可以在`/etc/cron.d/mytasks`中定义: + +``` +30 2 * * * root /usr/local/bin/my_script.sh +``` + + + +### 特殊符号 + +**常用的如下:** + +1. **星号(`*`)** + + - **含义**:匹配任何值,,即每一。 + - **示例**:在分钟字段中使用 `*` 表示每一分钟都运行一次任务。 + + ```sh + * * * * * /path/to/command + ``` + +2. **逗号(`,`)** + + - **含义**:指定一个列表值,列出所有指定值 a,b,c。 + - **示例**:在小时字段中使用 `0,6,12,18` 表示任务将在每天的0点、6点、12点和18点运行。 + + ```sh + 0 0,6,12,18 * * * /path/to/command + ``` + +3. **短横线(`-`)** + + - **含义**:指定一个范围值,开始-结尾。 + - **示例**:在日期字段中使用 `1-5` 表示任务将在每个月的1号到5号之间运行。 + + ```sh + 0 0 1-5 * * /path/to/command + ``` + +4. **斜杠(`/`)** + + - **含义**:指定步长值,即间隔。 + - **示例**:在分钟字段中使用 `*/15` 表示任务每15分钟运行一次。 + + ```sh + */15 * * * * /path/to/command + ``` + +5. **L(Last)** + + - **含义**:表示最后一个。可以在月的天数或星期几中使用。 + - **示例**:在日期字段中使用 `L` 表示每个月的最后一天。 + + ```sh + 0 0 L * * /path/to/command + ``` + + - **示例**:在星期几字段中使用 `5L` 表示每个月的最后一个星期五。 + + ```sh + 0 0 * * 5L /path/to/command + ``` + +### 例子和应用 + +1. **每月的第一个工作日** + + ```sh + 0 9 1W * * /path/to/command + ``` + +2. **每月的最后一天** + + ```sh + 0 23 28-31 * * [ "$(date +\%d -d tomorrow)" == "01" ] && /path/to/command + ``` + +3. **每个月的最后一个星期五** + + ```sh + 0 23 * * 5L /path/to/command + ``` + +4. **每隔10分钟** + + ```sh + */10 * * * * /path/to/command + ``` + +通过使用这些特殊符号,可以灵活地定义各种复杂的时间调度任务,使得cron更加强大和易用。 + +### 注意事项 + +1. **路径问题**:Cron任务运行时使用的环境变量可能与用户的交互式Shell不同,确保在脚本中使用绝对路径。 +2. **权限问题**:确保脚本具有可执行权限,并且Cron任务的用户有权限执行该脚本。 + +# 作业 + +### 周期任务练习 + +执行在家目录touch a.txt + +1. 每天3:00执行一次 + + ``` + 0 3 * * * /home/hlt/touch a.txt + ``` + + + +2. 每周六2:00执行 + + ``` + 0 2 * * 6 /home/hlt/touch a.txt + ``` + + + +3. 每周六1:05执行 + + ``` + 5 1 * * 6 /home/hlt/touch a.txt + ``` + + + +4. 每周六1:25执行 + + ``` + 25 1 * * 6 /home/hlt/touch a.txt + ``` + + + +5. 每天8:40执行 + + ``` + 40 8 * * * /home/hlt/touch a.txt + ``` + + + +6. 每天3:50执行 + + ``` + 50 3 * * * /home/hlt/touch a.txt + ``` + + + +7. 每周一到周五的3:40执行 + + ``` + 40 3 * * 1-5 /home/hlt/touch a.txt + ``` + + + +8. 每周一到周五的3:41开始,每10分钟执行一次 + + ``` + 41/10 3 * * 1-5 /home/hlt/touch a.txt + ``` + + + +9. 每天的10:31开始,每2小时执行一次 + + ``` + 31 10/2 * * * /home/hlt/touch a.txt + ``` + + + +10. 每周一到周三的9:30执行一次 + + ``` + 30 9 * * 1-3 /home/hlt/touch a.txt + ``` + + + +11. 每周一到周五的8:00,每周一到周五的9:00执行一次 + + ``` + 0 8,9 * * 1-5 /home/hlt/touch a.txt + ``` + + + +12. 每天的23:45分执行一次 + + ``` + 45 23 * * * /home/hlt/touch a.txt + ``` + + + +13. 每周三的23:45分执行一次 + + ``` + 45 23 * * 3 /home/hlt/touch a.txt + ``` + + + +14. 每周一到周五的9:25到11:35、13:00到15:00之间,每隔10分钟执行一次 + + ``` + 25-59/10 9 * * 1-5 /home/hlt/touch a.txt + 0/10 10 * * 1-5 /home/hlt/touch a.txt + 0-35/10 11 * * 1-5 /home/hlt/touch a.txt + + 0/10 13-15 * * 1-5 /home/hlt/touch a.txt + ``` + + + +15. 每周一到周五的8:30、8:50、9:30、10:00、10:30、11:00、11:30、13:30、14:00、14:30、5:00分别执行一次 + + ``` + 30 8,9,10,11,13,14 * * 1-5 /home/hlt/touch a.txt + 50 8 * * 1-5 /home/hlt/touch a.txt + 0 5,10,11,14 * * 1-5 /home/hlt/touch a.txt + ``` + + + +16. 每天16:00、10:00执行一次 + + ``` + 0 10,16 * * * /home/hlt/touch a.txt + ``` + + + +17. 每天8:10、16:00、21:00分别执行一次 + + ``` + 10 8 * * * /home/hlt/touch a.txt + 0 16,21 * * * /home/hlt/touch a.txt + ``` + + + +18. 每天7:47、8:00分别执行一次 + + ``` + 47 7 * * * /home/hlt/touch a.txt + 0 8 * * * /home/hlt/touch a.txt + ``` + + \ No newline at end of file -- Gitee