安裝crontab: yum install crontabs
說(shuō)明:
/sbin/service crond start //啟動(dòng)服務(wù)
/sbin/service crond stop //關(guān)閉服務(wù)
/sbin/service crond restart //重啟服務(wù)
/sbin/service crond reload //重新載入配置
查看crontab服務(wù)狀態(tài): service crond status
手動(dòng)啟動(dòng)crontab服務(wù): service crond start
查看crontab服務(wù)是否已設(shè)置為開機(jī)啟動(dòng),執(zhí)行命令: ntsysv
加入開機(jī)自動(dòng)啟動(dòng): chkconfig --level 35 crond on
1,crontab命令
功能說(shuō)明:設(shè)置計(jì)時(shí)器。
語(yǔ) 法:crontab [-u <用戶名稱>][配置文件] 或 crontab [-u <用戶名稱>][-elr]
補(bǔ)充說(shuō)明:cron是一個(gè)常駐服務(wù),它提供計(jì)時(shí)器的功能,讓用戶在特定的時(shí)間得以執(zhí)行預(yù)設(shè)的指令或程序。只要用戶會(huì)編輯計(jì)時(shí)器的配置文件,就可以使 用計(jì)時(shí)器的功能。其配置文件格式如下:
Minute Hour Day Month DayOFWeek Command
參 數(shù):
-e 編輯該用戶的計(jì)時(shí)器設(shè)置。
-l 列出該用戶的計(jì)時(shí)器設(shè)置。
-r 刪除該用戶的計(jì)時(shí)器設(shè)置。
-u<用戶名稱> 指定要設(shè)定計(jì)時(shí)器的用戶名稱。
2,crontab 格式
基本格式 :
* * * * * command
分 時(shí) 日 月 周 命令
第1列表示分鐘1~59 每分鐘用*或者 */1表示
第2列表示小時(shí)1~23(0表示0點(diǎn))
第3列表示日期1~31
第4列 表示月份1~12
第5列標(biāo)識(shí)號(hào)星期0~6(0表示星期天)
第6列要運(yùn)行的命令
crontab特殊的符號(hào)說(shuō)明:
"*"代表所有的取值范圍內(nèi)的數(shù)字。特別要注意哦!
"/"代表每的意思,如"*/5"表示每5個(gè)單位
"-"代表從某個(gè)數(shù)字到某個(gè)數(shù)字
","分散的數(shù)字
crontab文件的一些例子:
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每晚的21:30重啟 apache。
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每月1、 10、22日的4 : 45重啟apache。
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每周六、周日的1 : 10重啟apache。
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示在每天18 : 00至23 : 00之間每隔30分鐘重啟apache。
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每星期六的11 : 00 pm重啟apache。
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
每一小時(shí)重啟apache
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
晚上11點(diǎn)到早上7點(diǎn)之間,每 隔一小時(shí)重啟apache
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
每月的4號(hào)與每周一到周三 的11點(diǎn)重啟apache
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
一月一號(hào)的4點(diǎn)重啟apache
*/30 * * * * /usr/sbin/ntpdate 210.72.145.44
每半小時(shí)同步一下時(shí)間
-------------------------------- 如何查看crontab的日志記錄 --------------------------------------------------------
昨天crontab中的同步任務(wù)沒有執(zhí)行,不知道是什么原因沒有執(zhí)行,貌似任務(wù)hang住了,想查詢一下crontab到底問(wèn)題出在哪里,或者h(yuǎn)ang在了什么地方。
1. linux
看 /var/log/cron這個(gè)文件就可以,可以用tail -f /var/log/cron觀察
2. unix
在 /var/spool/cron/tmp文件中,有croutXXX001864的tmp文件,tail 這些文件就可以看到正在執(zhí)行的任務(wù)了。
3. mail任務(wù)
在 /var/spool/mail/root 文件中,有crontab執(zhí)行日志的記錄,用tail -f /var/spool/mail/root 即可查看最近的crontab執(zhí)行情況。