如何设置Linux Crontab 定时任务
我们经常使用的是crontab命令是cron table的简写,它是cron的配置文件,也可以叫它作业列表,我们可以在以下文件夹内找到相关配置文件。
/var/spool/cron/ 目录下存放的是每个用户包括root的crontab任务,每个任务以创建者的名字命名
style="margin-bottom: 0.15in; border: none; padding: 0in; line-height: 150%; page-break-inside: auto; orphans: 2; widows: 2; page-break-before: auto; page-break-after: auto;">/etc/crontab 这个文件负责调度各种管理和维护任务。
style="margin-bottom: 0.15in; border: none; padding: 0in; line-height: 150%; page-break-inside: auto; orphans: 2; widows: 2; page-break-before: auto; page-break-after: auto;">/etc/cron.d/ 这个目录用来存放任何要执行的crontab文件或脚本。
style="margin-bottom: 0.15in; border: none; padding: 0in; line-height: 150%; page-break-inside: auto; orphans: 2; widows: 2; page-break-before: auto; page-break-after: auto;"> 我们还可以把脚本放在/etc/cron.hourly、/etc/cron.daily、/etc/cron.weekly、/etc/cron.monthly目录中,让它每小时/天/星期、月执行一次。
crontab的使用我们常用的命令如下:
crontab [-u username] //省略用户表表示操作当前用户的crontab
-e (编辑工作表)
-l (列出工作表里的命令)
-r (删除工作作)
我们用crontab -e进入当前用户的工作表编辑,是常见的vim界面。每行是一条命令。
crontab的命令构成为 时间 动作,其时间有分、时、日、月、周五种,操作符有
-
style="margin-bottom: 0in; border: none; padding: 0in; line-height: 150%; page-break-inside: auto; orphans: 2; widows: 2; page-break-before: auto; page-break-after: auto;">*取值范围内的所有数字
-
style="margin-bottom: 0in; border: none; padding: 0in; line-height: 150%; page-break-inside: auto; orphans: 2; widows: 2; page-break-before: auto; page-break-after: auto;">/每过多少个数字
-
style="margin-bottom: 0in; border: none; padding: 0in; line-height: 150%; page-break-inside: auto; orphans: 2; widows: 2; page-break-before: auto; page-break-after: auto;">-从X到Z
-
style="margin-bottom: 0.15in; border: none; padding: 0in; line-height: 150%; page-break-inside: auto; orphans: 2; widows: 2; page-break-before: auto; page-break-after: auto;">,散列数字
* * * * * 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重启smb30 21 * * * /etc/init.d/smb restart
实例7:每月1、10、22日的4 : 45重启smb45 4 1,10,22 * * /etc/init.d/smb restart
实例8:每周六、周日的1 : 10重启smb10 1 * * 6,0 /etc/init.d/smb restart
实例9:每天18 : 00至23 : 00之间每隔30分钟重启smb0,30 18-23 * * * /etc/init.d/smb restart
实例10:每星期六的晚上11 : 00 pm重启smb0 23 * * 6 /etc/init.d/smb restart
实例11:每一小时重启smb* */1 * * * /etc/init.d/smb restart
实例12:晚上11点到早上7点之间,每隔一小时重启smb* 23-7/1 * * * /etc/init.d/smb restart
版权保护: 本文「如何设置Linux Crontab 定时任务」由 云主机配置专家 原创,转载请保留链接: https://www.miandns.com/docs/system/1004.html