【Linux碎知识点4】Linux自带的crontab定时任务

Linux定时任务crontab

crontab的服务进程名为crond,英文意为周期任务。顾名思义,crontab在Linux主要用于周期定时任务管理。类似crontab的工具还有at和anacrontab,但具体使用场景不同。

允许用户提交、编辑或删除相应的作业。每一个用户都可以有一个crontab文件来保存调度信息。系统管理员可以通过==/etc/cron.deny== 和 /etc/cron.allow这两个文件来禁止或允许用户拥有自己的crontab文件。

  • 当系统中有 /etc/cron.allow 文件时,只有写入此文件的用户可以使用 crontab 命令,没有写入的用户不能使用 crontab 命令。同样,如果有此文件,/etc/cron.deny 文件会被忽略,因为 /etc/cron.allow 文件的优先级更高。
  • 当系统中只有 /etc/cron.deny 文件时,写入此文件的用户不能使用 crontab 命令,没有写入文件的用户可以使用 crontab 命令。
  • 这个规则基本和 at 命令的规则一致,同样是 /etc/cron.allow 文件比 /etc/cron.deny 文件的优先级高,Linux 系统中默认只有 /etc/cron.deny 文件。

cron 系统调度进程,通常安装操作系统后,默认已启动crond服务。 可以使用它在每天的非高峰负荷时间段运行作业,或在一周或一月中的不同时段运行。cron是系统主要的调度进程,可以在无需人工干预的情况下运行作业。root用户进程

关于crontab的用途很多,如

  • 定时系统检测;
  • 定时数据采集;
  • 定时日志备份;
  • 定时更新数据缓存;
  • 定时生成报表;

当然,更多使用场景是要以视具体情况而定了。毕竟是工具通常都是常用规则总结而成的产物。

(1)检查系统是否安装crontab
  • 检验是否安装
[root@hadoop04 ~]# rpm -qa | grep crontab
crontabs-1.11-6.20121102git.el7.noarch
[root@hadoop04 ~]# rpm -qa | grep cron
cronie-anacron-1.4.11-19.el7.x86_64
crontabs-1.11-6.20121102git.el7.noarch
cronie-1.4.11-19.el7.x86_64
  • 安装(没操作)
yum -y install vixie-cron
yum -y install crontabs
(2)启停命令

Centos7:

[root@hadoop04 ~]# systemctl status crond #查看状态
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2022-08-08 15:24:10 CST; 8min ago
 Main PID: 6896 (crond)
    Tasks: 1
   CGroup: /system.slice/crond.service
           └─6896 /usr/sbin/crond -n

Aug 08 15:24:10 hadoop04 systemd[1]: Started Command Scheduler.
Aug 08 15:24:10 hadoop04 crond[6896]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 86% if used.)
Aug 08 15:24:10 hadoop04 crond[6896]: (CRON) INFO (running with inotify support)
[root@hadoop04 ~]# systemctl status crond.service #查看状态
[root@hadoop04 ~]# systemctl start crond.service #启动进程
[root@hadoop04 ~]# systemctl stop crond.service #停止进程
[root@hadoop04 ~]# systemctl reload crond.service #重新加载配置
(3)系统定时调度和用户定时调度
  • 系统定时调度

全局配置文件在/etc目录下:

[root@hadoop04 ~]# ls -l /etc | grep "cron"
-rw-------.  1 root root      541 Apr 11  2018 anacrontab
drwxr-xr-x.  2 root root       54 Jul  4  2021 cron.d	#系统自动定期需要做的任务
drwxr-xr-x.  2 root root       57 Jul  4  2021 cron.daily	#每天执行一次的job
-rw-------.  1 root root        0 Apr 11  2018 cron.deny	#用于控制不让哪些用户使用Crontab的功能
drwxr-xr-x.  2 root root       41 Jul  4  2021 cron.hourly	#每小时执行一次的job
drwxr-xr-x.  2 root root        6 Jun 10  2014 cron.monthly	#每月执行一次的job
-rw-r--r--.  1 root root      451 Jun 10  2014 crontab	#设定系统定时任务执行文件
drwxr-xr-x.  2 root root        6 Jun 10  2014 cron.weekly	#每周执行一次的job
  • 用户定时调度

每个用户都有自己的cron配置文件,通过crontab -e 就可以编辑。在crontab 文件都被保存在/var/spool/cron目录中。其文件名与用户名一致。

  • 日志信息
[root@hadoop04 ~]# ll /var/log/cron
-rw------- 1 root root 10397 Aug 10 15:06 /var/log/cron
[root@hadoop04 ~]# tailf /var/log/cron
Aug 10 15:01:01 hadoop04 run-parts(/etc/cron.hourly)[7811]: starting 0anacron
Aug 10 15:01:01 hadoop04 anacron[7826]: Anacron started on 2022-08-10
Aug 10 15:01:01 hadoop04 run-parts(/etc/cron.hourly)[7828]: finished 0anacron
Aug 10 15:01:01 hadoop04 anacron[7826]: Will run job `cron.daily' in 18 min.
Aug 10 15:01:01 hadoop04 anacron[7826]: Will run job `cron.weekly' in 38 min.
Aug 10 15:01:01 hadoop04 anacron[7826]: Jobs will be executed sequentially
Aug 10 15:01:01 hadoop04 run-parts(/etc/cron.hourly)[7811]: starting mcelog.cron
Aug 10 15:01:01 hadoop04 run-parts(/etc/cron.hourly)[7834]: finished mcelog.cron
Aug 10 15:06:23 hadoop04 crontab[7977]: (root) LIST (root)
Aug 10 15:06:28 hadoop04 crontab[7978]: (root) LIST (hadoop)


(4)crontab格式
[root@hadoop04 ~]# cat /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

前四行是用来配置crond任务运行的环境变量
第一行SHELL变量指定了系统要使用哪个shell,这里是bash
第二行PATH变量指定了系统执行命令的路径
第三行MAILTO变量指定了crond的任务执行信息将通过电子邮件发送给root用户;如果MAILTO变量的值为空,则表示不发送任务执行信息给用户

符号含义:
①星号(*):代表每的意思,例如month字段如果是星号,则表示每月都执行该命令操作。
②逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”
③中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”
④正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次。
(5)用户定时常用命令
crontab -l	#展示该用户的定时任务清单
crontab -e	#编辑该用户的定时任务
crontab -r	#删除该用户的全部定时任务,相当于/var/spool/cron目录中删除用户的crontab文件
crontab -i	#在删除用户的crontab文件时给确认提示
crontab -l > $PATH/mycron #相当于备份crontab文件
crontab -u root -e	#-u指定用户编辑定时任务
(6)注意事项
  • ①定时任务里面的程序脚本尽量用全路径
  • ②定时任务要加注释
  • ③避免不必要的程序以及命令输
  • ④在定义时间时,日期和星期最好不要在一条定时任务中出现,因为它们都以天为单位
  • ⑤当crontab突然失效时,可以尝试/etc/init.d/crond` `restart解决问题。或者查看日志看某个job有没有执行/报错tail -f /var/log/cron
  • ⑥千万别乱运行crontab` `-r。它从Crontab目录(/var/spool/cron)中删除用户的Crontab文件。删除了该用户的所有crontab``都没了。
  • ⑦在crontab中%是有特殊含义的,表示换行的意思。如果要用的话必须进行转义%,如经常用的date` `‘+%Y%m%d’在crontab里是不会执行的,应该换成date ‘+%Y%m%d’
(7)经典案例
  • ①有一个/opt/backup.sh脚本,如何在10月的每天早上06到18每3小时执行并在13点单独执行一次。
00 06-18,/3,13 * 10 * sh /opt/backup.sh >/dev/null 2>&1
  • ②设计一个shell程序,每月第一天执行
00 00 01 * *   shell.sh  >/dev/null 2>&1
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值