crontab#
可以使用「crontab」指令控制工作排程。
Edit, List, Remove#
1crontab -e # 編輯排程
2crontab -l # 列出清單
3crontab -r # 刪除排程
如果權限允許,那也可以在上面所述的指令下,加上-u <user>參數指定欲更動何使用者的排程。
Format#
1# Edit this file to introduce tasks to be run by cron.
2#
3# Each task to run has to be defined through a single line
4# indicating with different fields when the task will be run
5# and what command to run for the task
6#
7# To define the time you can provide concrete values for
8# minute (m), hour (h), day of month (dom), month (mon),
9# and day of week (dow) or use '*' in these fields (for 'any').
10#
11# Notice that tasks will be started based on the cron's system
12# daemon's notion of time and timezones.
13#
14# Output of the crontab jobs (including errors) is sent through
15# email to the user the crontab file belongs to (unless redirected).
16#
17# For example, you can run a backup of all your user accounts
18# at 5 a.m every week with:
19# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
20#
21# For more information see the manual pages of crontab(5) and cron(8)
22#
23# m h dom mon dow command
在使用編輯參數「-e」後,會用預設編輯器編輯一個如上的暫存檔案。
檔案中預設是沒有排程的,但有一些註解。
直接在註解下方新增一行,照著格式輸入設定即可。
可以在最後一行看到其使用格式「m h dom mon dow command」,其含意如下:
| Format | Range | Example |
|---|---|---|
m, Minute, 分 | 0-59 | 每小時 30 分 30 * * * * Command |
h, Hour, 時 | 0-23 | 每天 1 時 30 分 30 1 * * * Command |
dom, Day of Month, 日 | 1-31 | 每月的 5 號 12 時 0 分 0 12 5 * * Command |
mon, Month, 月 | 0-12 | 每年 7 月 14 的 0 時 1 分 1 0 14 7 * Command |
dow, Day of Week, 星期 | 0-7 (0,7 皆表示星期天) | 每周一的 0 時 0 分 0 0 * * 1 Command |
command, 指令 | <command> | 每周 2 的 23 時 0 分執行 backup 指令 0 23 * * 2 backup |
特殊值符號:
| Sign | Explanation | Example |
|---|---|---|
* | 任意時段 | 每天 12 時 0 分 0 12 * * * Command |
, | 分隔多個時段 | 每天的 9 時 0 分及 20 時 0 分 0 9,12 * * * Command |
- | 時間範圍 | 每天 7 時至 10 時,當 0 分時 0 7-10 * * * Command |
n/n | 循環時段 | 每週 2 的每小時 0 分 0 */1 * * 2 Command |
這邊要特別提的是
循環時段的部分,使用「/」區隔,前面的值表示範圍,後面的值表示頻率。
舉例來說,當想要30~40單位時,每1單位執行一次,則為「30-40/1」。
或者也可以單純指定每1單位就執行一次,不限制範圍,則可以寫作「*/1」。
- example:
每天12:00至18:00間,每小時的30分時執行一次 command:30 12-18/1 * * * command
別名:
| Special Strings | Explanation | Example |
|---|---|---|
@yearly | 每年 1 月 1 日的 00:00 (等同@annually、0 0 1 1 *) | @yearly command |
@annually | 每年 1 月 1 日的 00:00 (等同0 0 1 1 *) | @annually command |
@monthly | 每月 1 日的 00:00 (等同0 0 1 * *) | @monthly command |
@weekly | 每週日(或週一,依系統設定)的 00:00 (等同0 0 * * 0) | @weekly command |
@daily | 每天凌晨 00:00 (等同 @midnight、0 0 * * *) | @daily command |
@midnight | 每天凌晨 00:00 (等同 @daily、0 0 * * *) | @daily command |
@hourly | 每小時的第 0 分鐘 (等同0 * * * *) | @hourly command |
另外,還有一個特殊的別名「@reboot」,
如同名稱般表示在開機時執行,例如每次開機執行echo 'hello' > /tmp/hi:
1@reboot echo 'hello' > /tmp/hi
Permission#
可以透過創建White List檔案來指定哪些使用者可以創建排程;
或者透過創建Black List檔案來指定哪些使用者不能創建排程。
- White List:
/etc/crontab.allow - Black List:
/etc/crontab.deny
程式的判定會先查看是否存在White List的檔案/etc/crontab.allow
若不存在,則會查看Black List的檔案/etc/crontab.deny
例如僅允許使用者admin及admin2執行 crontab 指令,則編輯/etc/crontab.allow:
1# /etc/crontab.allow
2admin
3admin2
若僅不允許使用者guest及guest2執行 crontab 指令,則先確保/etc/crontab.allow不存在,接著編輯/etc/crontab.deny:
1# /etc/crontab.deny
2guest
3guest2
/etc/init.d/cron#
輸入/etc/init.d/cron並透過status, start, stop, restart等參數
來對 Cron 服務狀態查看, 啟動, 停止, 重新啟動。
方便的小工具#
這個網站可以方便學習 crontab 的格式:
https://crontab.guru/

References#
《循環執行的工作排程 - dywang》https://dywang.csie.cyut.edu.tw/dywang/linuxSystem/node71.html