13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

Linux中使用crond工具创建定时任务的方法

前言

操作步骤

(1) 检查crond工具是否安装

yum list installed | grep crontabs

若未安装,则使用如下所示命令安装

sudo yum install crontabs

(2) 检查crond服务是否开启由于是CentOS7所以使用 systemctl 命令,而非 service 命令

systemctl status crond.service

若未开启,则使用如下所示命令开启服务

sudo systemctl start crond.service

(3) 使用crond工具创建任务计划crontab命令使用方法

Usage:
crontab [options] file
crontab [options]
crontab -n [hostname]

Options:
-u define user
-e edit user’s crontab
-l list user’s crontab
-r delete user’s crontab
-i prompt before deleting
-n set host in cluster to run users’ crontabs
-c get host in cluster to run users’ crontabs
-s selinux context
-x enable debugging
# 注意 crontab -r 是删除用户的所有定时任务(慎用!)

可以通过 /etc/crontab 文件查看任务定义格式和设定任务执行环境

以“每分钟定时将日期写入指定文件中”为例

方法1:使用crontab命令编辑当前用户定时任务(立即生效)**

crontab -e

在编辑器中插入如下指令(注意此时不要追加用户,否则无法执行,因为此方法是直接设置当前用户的定时任务)

*/1 * * * * date >> /home/TomAndersen/currentDate

检查插入结果

[tomandersen@hadoop101 bin]$ crontab -l
*/1 * * * * date >> /home/TomAndersen/currentDate

方法2:编辑 /etc/crontab 文件,按照格式插入(生效较慢)**

*/1 * * * * tomandersen date >> /home/TomAndersen/currentDate

(4) 检查是否设置成功

[tomandersen@hadoop101 bin]$ cat /home/TomAndersen/currentDate
2020年 02月 09日 星期日 18:12:01 CST
2020年 02月 09日 星期日 18:13:01 CST
2020年 02月 09日 星期日 18:14:01 CST
2020年 02月 09日 星期日 18:15:01 CST
2020年 02月 09日 星期日 18:16:02 CST
2020年 02月 09日 星期日 18:17:01 CST
2020年 02月 09日 星期日 18:18:01 CST
2020年 02月 09日 星期日 18:19:01 CST
2020年 02月 09日 星期日 18:20:01 CST

总结

以上所述是小编给大家介绍的Linux中使用crond工具创建定时任务,希望对大家有所帮助!

成都创新互联科技有限公司,经过多年的不懈努力,公司现已经成为一家专业从事IT产品开发和营销公司。广泛应用于计算机网络、设计、SEO优化、关键词排名等多种行业!


本文题目:Linux中使用crond工具创建定时任务的方法
本文链接:http://cdbrznjsb.com/article/cdgoejo.html

其他资讯

让你的专属顾问为你服务