Centos7定时任务Crond安装、配置

Centos7定时任务Crond安装、配置

安装

一般情况下crond、crontab是安装好的,这里主要提一下万一没有crontab你别想着直接用yum去安装crond或者crontab因为你会发现找不到包,其实定时任务的包是cronie。

[root@localhost log]# yum install -y cronie

关于 crond 和 crondtab 常用命令

  • crond常用命令
# 启动服务
[root@localhost log]# systemctl start  crond

# 停止服务
[root@localhost log]# systemctl stop crond

# 重启服务
[root@localhost log]# systemctl restart crond

# 重载配置文件
[root@localhost log]# systemctl reload  crond

# 查看状态
[root@localhost log]# systemctl status  crond
  • crontab常用命令
# 设定某个用户的cron服务
crontab -u

# 显示crontab文件(显示已设置的定时任务)
crontab -l

# 编辑crontab文件(编辑定时任务)
crontab -e

# 删除crontab文件(删除定时任务)
crontab -r

# 删除crontab文件提醒用户(删除定时任务)
crontab -i

配置

  1. 直接修改配置文件
    将定时任务添加到 /etc/crontab 的最后一行
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# 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

# 此处就是配置的定时任务
0,30 * * * * root /opt/databak/backup.sh

其中的时间格式可以为

* :表示任意的时刻;如小时位 * 则表示每个小时
n :表示特定的时刻;如小时位 5 就表示5时
n,m :表示特定的几个时刻;如小时位 1,10 就表示1时和10时
n-m :表示一个时间段;如小时位 1-5 就表示1到5点
*/n : 表示每隔多少个时间单位执行一次;如小时位 */1 就表示每隔1个小时执行一次命令,也可以写成 1-23/1
  1. 使用crontab -e 命令编辑
[root@localhost log]# crontab -e 0,30 * * * * root /opt/databak/backup.sh
  1. 重载配置
[root@localhost log]# systemctl reload crond

遇到的问题

  1. 手动删除日志文件后,无法在输出日志

    解决办法:

# 日志文件 cron 的位置在 /etc/log/
# 重启日志服务 
[root@localhost log]# systemctl restart rsyslog
  1. 在 crontab 中添加定时任务时,使用了生成工具生成的 cron 表达式。但是,这样并不行,这种格式是不被允许的,还是需要根据 crontab 文件中的说明进行配置。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值