08、crond计划任务

一、定时任务介绍

1、定时任务

设定某个日期或时间周期性执行指令。

2、crond

Crond是Linux系统中用来定期执行命令或脚本的一种服务软件,一般情况下,我们安装完CentOS操作系统之后,默认便会启动Crond任务调度服务。

Crond服务会定期(默认每分钟检查一次)检查系统中是否有要执行的任务工作,如果有,便会根据其预先设定的定时任务规则自动执行该定时任务工作,这个Crond定时任务服务就相当于我们平时早起使用的闹钟一样。

3、crond日志文件

/var/log/cron

4、定时任务的两种使用情况

(1)系统级别的定时任务:
临时文件清理例/tmp和/var/tmp等、系统信息采集、日志文件切割

(2)用户级别的定时任务:
定时向互联网同步时间、定时备份系统配置文件、定时备份数据库的数据

二、定时任务管理

1、系统级计划任务

  • 定义方式一:编辑文件 /etc/crontab
[root@localhost ~]# vim /etc/crontab
[root@localhost ~]# 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
分钟
小时
日
月
周
  • 定义方式二:把脚本放到该目录下
/etc/cron.hourly/ # 系统定时任务每个小时运行这个目录里的内容
/etc/cron.daily/ # 系统定时任务每天运行这个目录里的内容
/etc/cron.weekly/ # 系统定时任务每周运行这个目录里的内容
/etc/cron.monthly/ # 系统定时任务每月运行这个目录里的内容

2、用户级计划任务

[root@localhost ~]# crontab -e # 创建计划任务
* * * * * /bin/ls
[root@localhost ~]# crontab -l # 查看计划任务
* * * * * /bin/ls
[root@localhost ~]# ls /var/spool/cron/
root
[root@localhost ~]# cat /var/spool/cron/root # 都是写在这里呢
* * * * * /bin/ls
[root@localhost ~]# tail /var/log/cron # 查看日志
[root@localhost ~]# crontab -u egon -e
# /etc/cron.deny 是定时任务的黑名单,使用root将需要拒绝的用户加入/etc/cron.deny
[root@localhost ~]# echo "egon" >> /etc/cron.deny
[root@localhost ~]# su - egon # 登陆该普通用户,无法编写定时任务
上一次登录:四 8月 13 19:44:10 CST 2020pts/1 上
[egon@localhost ~]$ crontab -e
You (egon) are not allowed to use this program (crontab)
See crontab(1) for more information

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值