crontab -u <-l|-r|-e>
-l 列出任務
-r 刪除所有任務
-e 編輯任務 (進入編輯器)
Minute Hour Day Month Week Command
分 時 日 月 周 命令
0-59 0-23 1-31 1-12 0-6 command
* = 所有取值/n = 每 n 步長a-b = 範圍a,b,c = 離散列表| 表達式 | 含義 |
|---|---|
5 * * * * ls | 每小時第 5 分執行 |
30 5 * * * ls | 每天 05:30 執行 |
30 7 8 * * ls | 每月 8 號 07:30 |
30 5 8 6 * ls | 每年 6 月 8 日 05:30 |
30 6 * * 0 ls | 每週日 06:30 (0/7=周日) |
30 3 10,20 * * ls | 每月 10/20 號 03:30 |
25 8-11 * * * ls | 每天 08-11 點第 25 分 |
*/15 * * * * ls | 每 15 分鐘 (0,15,30,45) |
30 6 */10 * * ls | 每月 1,11,21,31 號 06:30 |
/etc/crontab 與 /etc/cron.d/
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
run-parts 批量執行目錄下所有可執行腳本 (chmod +x)。/etc/cron.d/,格式同 /etc/crontab (需指定用戶欄)。/var/spool/cron/username,由 crontab -e 維護,不可直接編輯文件。cron 每分鐘讀取一次,修改即時生效,無需重啟服務。
/etc/rc.d/init.d/crond {start|stop|status|reload|restart|condrestart}
# 或 systemctl {start|stop|restart|enable|disable} crond
chmod +x。/usr/bin/rsync)。>/dev/null 2>&1 靜默;>/tmp/log 2>&1 記錄。PATH、HOME。