Linux Cron 表達式

Linux Cron表達式

Cron表達式的定義

Linux的Cron表達式支持分鐘、小時、月的某天、月份、周當中的某天,最小的粒度到分鐘、最大的粒度到月份和周的某天。

編輯一個linux定時任務:

crontab -e

每位的意義

* * * * * 要執行的命令
----------------
| | | | |
| | | | ---- 周當中的某天 (0 - 7) (週日爲 0 或 7)
| | | ------ 月份 (1 - 12)
| | -------- 一月當中的某天 (1 - 31)
| ---------- 小時 (0 - 23)
------------ 分鐘 (0 - 59)

取值範圍

代表意義 分鐘 小時 日期 月份 指令
數字範圍 0-59 0-23 1-31 1-12 0-7 定時執行的命令

表達式中的特殊字符

特殊字    符 代表意義
*(星號) 代表任何時刻!例如每天每月每週都可以用*
,(逗號) 代表分隔時間點。例如在每天的3點和6點執行命令:
0 3,6 * * * command
-(中劃線) 代表一段時間間隔。例如 8 點到 12 點之間的每小時的 20 分執行命令:
20 8-12 * * * command
/n(斜線) n表示一個整數,代表每個n間隔。例如每五分鐘執行命令:
*/5 * * * * command

表達式應用示例

# 每天晚上1點調用/home/testuser/test.sh
0 1 * * * /home/testuser/test.sh 


# 每10鐘調用一次/home/testuser/test.sh
/10 * * * /home/testuser/test.sh 

# 每晚的21:30重啓apache
30 21 * * * /usr/local/etc/rc.d/lighttpd restart 

# 在每天18:00至23:00之間每隔30分鐘重啓apache
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart 

# 每月1、10、22日的4 : 45重啓apache
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart 

# 每週六、週日的1:10重啓apache
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章