admin 发表于 2023-3-2 19:25:45

CentOS Crontab(定时任务)

<p>安装crontab:</p>
<div class="cnblogs_code">
<pre>yum install crontabs</pre>
</div>
<p>说明:</p>
<div class="cnblogs_code">
<pre>service crond start //启动服务
service crond stop //关闭服务
service crond restart //重启服务
service crond reload //重新载入配置
查看crontab服务状态:service crond status
手动启动crontab服务:service crond start</pre>
</div>
<p>查看crontab服务是否已设置为开机启动,执行命令:ntsysv</p>
<p>加入开机自动启动:</p>
<div class="cnblogs_code">
<pre>chkconfig crond on</pre>
</div>
<p>1、编辑命令</p>
<p>1)、在命令行输入: crontab -e 然后添加相应的任务,wq存盘退出</p>
<p>2)、直接编辑/etc/crontab 文件,即vi /etc/crontab,添加相应的任务</p>
<p>2、文件格式</p>
<p>Minute Hour Day Month DayofWeek CommandPath</p>
<p>3、参数说明</p>
<p>Minute:每个小时的第几分钟执行该任务;取值范围0-59</p>
<p>Hour:每天的第几个小时执行该任务;取值范围0-23</p>
<p>Day:每月的第几天执行该任务;取值范围1-31</p>
<p>Month:每年的第几个月执行该任务;取值范围1-12</p>
<p>DayOfWeek:每周的第几天执行该任务;取值范围0-6,0表示周末</p>
<p>CommandPath:指定要执行的程序路径</p>
<p>4、时间格式</p>
<p>* :表示任意的时刻;如小时位 * 则表示每个小时</p>
<p>n :表示特定的时刻;如小时位 5 就表示5时</p>
<p>n,m :表示特定的几个时刻;如小时位 1,10 就表示1时和10时</p>
<p>n-m :表示一个时间段;如小时位 1-5 就表示1到5点</p>
<p>*/n : 表示每隔多少个时间单位执行一次;如小时位 */1 就表示每隔1个小时执行一次命令,也可以写成 1-23/1</p>
<p>5、调度示例</p>
<div class="cnblogs_code">
<pre> * 1 * * * /opt/script/backup.sh :从1:0到1:59 每隔1分钟 执行
15 05 * * * /opt/script/backup.sh :05:15 执行
*/10 * * * * /opt/script/backup.sh :每隔10分 执行
0 17 * * 1 /opt/script/backup.sh :每周一的 17:00 执行
2 8-20/3 * * * /opt/script/backup.sh8:02,11:02,14:02,17:02,20:02 执行</pre>
</div>
<h2>实际举例</h2>
<p>crontab文件的一些例子:</p>
<div class="cnblogs_code">
<pre>30 21 * * * /etc/init.d/nginx restart             //每晚的21:30重启 nginx。
45 4 1,10,22 * * /etc/init.d/nginx restart      //每月1、 10、22日的4 : 45重启nginx。
10 1 * * 6,0 /etc/init.d/nginx restart            //每周六、周日的1 : 10重启nginx。
0,30 18-23 * * * /etc/init.d/nginx restart      //每天18 : 00至23 : 00之间每隔30分钟重启nginx。
0 23 * * 6 /etc/init.d/nginx restart            //每星期六的11 : 00 pm重启nginx。
* */1 * * * /etc/init.d/nginx restart             //每一小时重启nginx
* 23-7/1 * * * /etc/init.d/nginx restart          //晚上11点到早上7点之间,每 隔一小时重启nginx
0 11 4 * mon-wed /etc/init.d/nginx restart      //每月的4号与每周一到周三 的11点重启nginx
0 4 1 jan * /etc/init.d/nginx restart             //一月一号的4点重启nginx
*/30 * * * * /usr/sbin/ntpdate 210.72.145.20      //每半小时同步一下时间</pre>
</div>
                                        <p class="post-copyright">未经允许不得转载:<ahref="https://www.wangchao.info/">王超博客</a> &raquo; <ahref="https://www.wangchao.info/1279.html">CentOS Crontab(定时任务)</a></p>
页: [1]
查看完整版本: CentOS Crontab(定时任务)