查看crontab文件說明
[root@S478645 etc]# pwd
/etc
[root@S478645 etc]# cat crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 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
其中:
- minute: 表示分鐘,可以是從0到59之間的任何整數。
- hour:表示小時,可以是從0到23之間的任何整數。
- day:表示日期,可以是從1到31之間的任何整數。
- month:表示月份,可以是從1到12之間的任何整數。
- day of week:表示星期幾,可以是從0到7之間的任何整數,這裏的0或7代表星期日。
- user-name:執行用戶
- command:要執行的命令,可以是系統命令,也可以是自己編寫的腳本文件。
crond服務
/sbin/service crond start //啓動服務
/sbin/service crond stop //關閉服務
/sbin/service crond restart //重啓服務
/sbin/service crond reload //重新載入配置
查看crontab服務狀態:
service crond status
手動啓動crontab服務:
service crond start
查看crontab服務是否已設置爲開機啓動,執行命令:
ntsysv
加入開機自動啓動:
chkconfig –level 35 crond on
示例:
實例
每1分鐘執行一次command
* * * * * command
每小時的第3和第15分鐘執行
3,15 * * * * command
在上午8點到11點的第3和第15分鐘執行
3,15 8-11 * * * command
每隔兩天的上午8點到11點的第3和第15分鐘執行
3,15 8-11 */2 * * command
每個星期一的上午8點到11點的第3和第15分鐘執行
3,15 8-11 * * 1 command
每晚的21:30重啓smb
30 21 * * * /etc/init.d/smb restart
每月1、10、22日的4 : 45重啓smb
45 4 1,10,22 * * /etc/init.d/smb restart
每週六、週日的1:10重啓smb
10 1 * * 6,0 /etc/init.d/smb restart
每天18 : 00至23 : 00之間每隔30分鐘重啓smb
0,30 18-23 * * * /etc/init.d/smb restart
每星期六的晚上11:00 pm重啓smb
0 23 * * 6 /etc/init.d/smb restart
每一小時重啓smb
* */1 * * * /etc/init.d/smb restart
晚上11點到早上7點之間,每隔一小時重啓smb
* 23-7/1 * * * /etc/init.d/smb restart
每月的4號與每週一到週三的11點重啓smb
0 11 4 * mon-wed /etc/init.d/smb restart
一月一號的4點重啓smb
0 4 1 jan * /etc/init.d/smb restart
每小時執行/etc/cron.hourly目錄內的腳本
01 * * * * root run-parts /etc/cron.hourly