crontab是執行定時任務的一個命令。
以下所有描述基於RHEL AS OS
A. 重啓crontab服務
service crond restart
B. 任務設定格式描述
minute(0-59) hour(0-23) day(1-31) month(1-12) week(0-6) command
Opitions Explanation
-
* :Is treated as a wild card. Meaning any possible value.
-
*/5: Is treated as ever 5 minutes, hours, days, or months. Replacing the 5 with another numerical value will change this option.
-
2,4,6: Treated as an OR, so if placed in the hours, this could mean at 2, 4, or 6 o-clock.
-
9-17: Treats for any value between 9and 17. So if placed in day of month this would be days 9 through 17. Or if put in hours it would be between 9 and 5.
C. 相關設定
1.. 配置crond服務
/etc/crontab
2. 任務設定文件
如 /var/spool/cron/root
該文件不可直接修改,需要使用命令crontab -e
3. 禁止完成任務後發送email
系統默認情況下,會在相關任務出錯或者輸出數據時候發送一份email給對應用戶,禁止發送,只需要在所執行命令後面添加
...>/dev/null 2>&1
記錄log,命令後添加
...>/test/xxx.log
D. 實例
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每晚的21:30重啓lighttpd 。
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每月1、10、22日的4 : 45重啓lighttpd 。
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每週六、週日的1 : 10重啓lighttpd 。
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示在每天18 : 00至23 : 00之間每隔30分鐘重啓lighttpd 。
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每星期六的11 : 00 pm重啓lighttpd 。
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
每一小時重啓lighttpd
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
晚上11點到早上7點之間,每隔一小時重啓lighttpd
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
每月的4號與每週一到週三的11點重啓lighttpd
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
一月一號的4點重啓lighttpd