crontab命令是cron table的简写,它是cron的配置文件,也可以叫它作业列表,我们可以在以下文件夹内找到相关配置文件。
crontab [-u username] //省略用户表表示操作当前用户的crontab-e (编辑工作表)-l (列出工作表里的命令)-r (删除工作作)
我们用crontab -e进入当前用户的工作表编辑,是常见的vim界面。每行是一条命令。
crontab的命令构成为 时间+动作,其时间有分、时、日、月、周五种,操作符有
cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin# 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
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
*/30 * * * * mary /home/mary/wenshu/rsyncShare.sh
任务命令分为两个部分: 第一部分是cron表达是, 后面跟着sh命令
* * * * *
- - - - -
| | | | |
| | | | +----- 星期中星期几 (0 - 6) (星期天 为0)
| | | +---------- 月份 (1 - 12)
| | +--------------- 一个月中的第几天 (1 - 31)
| +-------------------- 小时 (0 - 23)
+------------------------- 分钟 (0 - 59)
cat /etc/crontabSHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root #发送邮件到账号
发送到外部邮箱,以qq邮箱为例在cat /etc/crontab将MAILTO=root 修改为你需要的外部邮箱账号且在crontab -e 的第一行添加MAILTO=外部邮箱账号并且配合linux发送邮件文章使用
* * * * * /root/date.sh # 每分钟执行一次date.sh
0 * * * * echo '-------------' >> /root/date.txt # 每小时0分的时候向date.txt文件中加入一句话
* 1 * * 0 rm -rf /root/date1 #每个星期的星期天的1点左右删除date1文件
#实例1:每1分钟执行一次myCommand
* * * * * myCommand
#实例2:每小时的第3和第15分钟执行
3,15 * * * * myCommand
#实例3:在上午8点到11点的第3和第15分钟执行
3,15 8-11 * * * myCommand
#实例4:每隔两天的上午8点到11点的第3和第15分钟执行
3,15 8-11 */2 * * myCommand
#实例5:每周一上午8点到11点的第3和第15分钟执行
3,15 8-11 * * 1 myCommand
#实例6:每晚的21:30重启smb
30 21 * * * /etc/init.d/smb restart
#实例7:每月1、10、22日的4 : 45重启smb
45 4 1,10,22 * * /etc/init.d/smb restart
#实例8:每周六、周日的1 : 10重启smb
10 1 * * 6,0 /etc/init.d/smb restart
#实例9:每天18 : 00至23 : 00之间每隔30分钟重启smb
0,30 18-23 * * * /etc/init.d/smb restart
#实例10:每星期六的晚上11 : 00 pm重启smb
0 23 * * 6 /etc/init.d/smb restart
#实例11:每一小时重启smb
0 */1 * * * /etc/init.d/smb restart
#实例12:晚上11点到早上7点之间,每隔一小时重启smb
0 23-7/1 * * * /etc/init.d/smb restart