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

发布时间:2020-08-28 05:22:07 作者:TomAndersen
来源:脚本之家 阅读:151

前言

操作步骤

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

yum list installed | grep crontabs

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

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

sudo yum install crontabs

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

systemctl status crond.service

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

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

sudo systemctl start crond.service

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

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

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

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


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

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

方法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工具创建定时任务,希望对大家有所帮助!

推荐阅读:
  1. MySql中创建定时任务
  2. 创建springboot定时任务的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

linux crond 定时任务

上一篇:java发送email一般步骤(实例讲解)

下一篇:python 删除字符串中连续多个空格并保留一个的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》