Posted on 2 Comments

Linux 設定排程 – crontab

查看與編輯 crontab

查看自己的 crontab
crontab -l
查看指定使用者的 crontab
sudo crontab -u gtwang -l
編輯 crontab 內容
crontab -e
編輯指定使用者的 crontab
crontab -u gtwang -e
刪除 crontab 內容
crontab -r

crontab設定檔撰寫教學

# For details see man 4 crontabs

# Example of job definition:
# .—————- minute (0 – 59)
# | .————- hour (0 – 23)
# | | .———- day of month (1 – 31)
# | | | .——- month (1 – 12) OR jan,feb,mar,apr …
# | | | | .—- day of week (0 – 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

範例如下:
# 每天早上 8 點 30 分執行
30 08 * * * /home/gtwang/script.sh –your –parameter

# 每週日下午 6 點 30 分執行
30 18 * * 0 yourcommand

# 每週日下午 6 點 30 分執行
30 18 * * Sun yourcommand

# 每年 6 月 10 日早上 8 點 30 分執行
30 08 10 06 * yourcommand

# 每月 1 日、15 日、29 日晚上 9 點 30 分各執行一次
30 21 1,15,29 * * yourcommand

# 每隔 10 分鐘執行一次
*/10 * * * * yourcommand

# 從早上 9 點到下午 6 點,凡遇到整點就執行
00 09-18 * * * yourcommand

crontab設定檔的特殊字元

特殊字元 代表意義
星號(* 代表接受任意時刻,例如若在月份那一欄填入星號,則代表任一月份皆可。
逗號(, 分隔多個不同時間點。例如若要指定 3:00、6:00 與 9:00 三個時間點執行指令,就可以在第二欄填入 3,6,9
減號(- 代表一段時間區間,例如若在第二欄填入 8-12 就代表從 8 點到 12 點的意思,也就是等同於 8,9,10,11,12
斜線加數字(/n n 代表數字,這樣寫的意思就是「每隔 n 的單位」的意思,例如若在第一欄填入 */5 就代表每間隔五分鐘執行一次的意思,也可以寫成 0-59/5

2 thoughts on “Linux 設定排程 – crontab

  1. Hello, 台湾岛人?

Comments are closed.